我一直在尝试为国际象棋程序加载棋子,该程序涉及加载一堆棋子预制件以加载到屏幕上,但由于某种原因它不起作用。这是代码
white_pawn = (GameObject) Resources.Load("wP.prefab", typeof(GameObject));
但它返回的只是
ArgumentNullException: Value cannot be null.
Parameter name: _unity_self
UnityEditor.SerializedObject.FindProperty (System.String propertyPath) (at <fe7039efe678478d9c83e73bc6a6566d>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field,
我尝试将其放入资源文件夹中:
Assets/Resources/wP.prefab
但它仍然不起作用。
有什么想法吗?
困惑时一定要查看文档 如果在这里查看如何正确使用此功能: https://docs.unity3d.com/ScriptReference/Resources.html
您会注意到文件扩展名被省略。
正确的线路例如是
private GameObject whitePawn;
private void Awake()
{
whitePawn = Resources.Load("wP") as GameObject;
}
The other casts are also redundant.