Table of contents
1. The object is not activated
①The object is not activated before running, so the object cannot be found at runtime;
②The object is deactivated by the script control during runtime, so the object cannot be found when it is used.
2. The object's parent object, grandparent object... is not activated
In Unity, if an object's parent object is not activated, all its child objects cannot be found.
Similarly, this situation will also cause the object to not be found at runtime.
Especially when using the GameObject.Find() function to find the GameObject globally, you need to pay special attention.
3. No mount script
Scripts inherited from MonoBehaviour are not attached to any GameObject on the Hierarchy panel.
In this case, the Awake(), Start() and Update() scripts in the script will not be executed, so naturally the required objects cannot be found.
4. Tracing back to the origin
For scripts that do not inherit from MonoBehaviour, you need to check whether the source script that calls the script is running (that is, it is mounted to an object on the Hierarchy panel, and its script component is activated)
5. Resource loading failed
The Resources.Load() function is used in the script, but there is no corresponding thing in the path it points to.
Check the Resources folder under the Assets folder for corresponding loaded resources.
6. Variable not assigned initial value
Only the variable is defined, but no initial value is assigned to it.
public Vector3 vec = new Vector3(0,0,0);
public string[] strs = new string[5];
int index = 0;
float ff = 0.0f;
The script instance is only defined, but not instantiated.
For example, if you define an instance Demo inherited from the Monobehavior script, you need to instantiate it when it is Awake, so as not to report empty when its method is referenced elsewhere.
public class Demo : MonoBehaviour
{
public static Demo Instance;
private void Awake()
{
Instance = this;
}