Skip to main content
added 400 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

Note: Be carefull, GameObject.Find, GameObject.FindWithTag, GameObject.FindObjectOfType only return gameObjects that are enabled in the hierarchy when the function is called.

Trying to use the result of a getter that's returning null:

Trying to use the result of a getter that's returning null:

Note: Be carefull, GameObject.Find, GameObject.FindWithTag, GameObject.FindObjectOfType only return gameObjects that are enabled in the hierarchy when the function is called.

Trying to use the result of a getter that's returning null:

deleted 255 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

Retrieving a component that isn't attached to the GameObject and then, trying to manipulatingmanipulate it:

private void Awake ()
{
    // Here, you try to get the Collider component attached to your gameobject
    Collider collider = gameObject.GetComponent<Collider>();

    // But, if you haven't any collider attached to your gameobject,
    // GetComponent won't find it and will return null, and you will get the exception.
    collider.enabled = false ;
}

Accessing an element of a non-initialized array

private GameObject[] myObjects ; // Uninitialized array

private void Start()
{
    for( int i = 0 ; i < myObjects.Length ; ++i )
        Debug.Log( myObjects[i].name ) ;
}

Accessing a GameObject that doesntdoesn't exist:

Retrieving a component that isn't attached to the GameObject and then, trying to manipulating it:

private void Awake ()
{
    // Here, you try to get the Collider component attached to your gameobject
    Collider collider = gameObject.GetComponent<Collider>();

    // But, if you haven't any collider attached to your gameobject,
    // GetComponent won't find it and will return null, and you will get the exception.
    collider.enabled = false ;
}

Accessing an element of a non-initialized array

private GameObject[] myObjects ; // Uninitialized array

private void Start()
{
    for( int i = 0 ; i < myObjects.Length ; ++i )
        Debug.Log( myObjects[i].name ) ;
}

Accessing a GameObject that doesnt exist:

Retrieving a component that isn't attached to the GameObject and then, trying to manipulate it:

private void Awake ()
{
    // Here, you try to get the Collider component attached to your gameobject
    Collider collider = gameObject.GetComponent<Collider>();

    // But, if you haven't any collider attached to your gameobject,
    // GetComponent won't find it and will return null, and you will get the exception.
    collider.enabled = false ;
}

Accessing a GameObject that doesn't exist:

added 646 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

Manipulating a GameObject / Component that has not been specified in the inspector

If you try to get a component that isn't there and then, try to access it:Retrieving a component that isn't attached to the GameObject and then, trying to manipulating it:

Accessing a GameObject that doesnt exist:Accessing an element of a non-initialized array

private GameObject[] myObjects ; // Uninitialized array

private void Start()
{
    for( int i = 0 ; i < myObjects.Length ; ++i )
        Debug.Log( myObjects[i].name ) ;
}

Accessing a GameObject that doesnt exist:

Trying to use the result of a getter that's returning null:Trying to use the result of a getter that's returning null:

Less common, but annoying if you don't know it about C# delegates:Accessing an element of a non-initialized array

private GameObject[] myObjects ; // Uninitialized array

private void Start()
{
    for( int i = 0 ; i < myObjects.Length ; ++i )
        Debug.Log( myObjects[i].name ) ;
}

Less common, but annoying if you don't know it about C# delegates:

If you try to get a component that isn't there and then, try to access it:

Accessing a GameObject that doesnt exist:

Trying to use the result of a getter that's returning null:

Less common, but annoying if you don't know it about C# delegates:

Manipulating a GameObject / Component that has not been specified in the inspector

Retrieving a component that isn't attached to the GameObject and then, trying to manipulating it:

Accessing an element of a non-initialized array

private GameObject[] myObjects ; // Uninitialized array

private void Start()
{
    for( int i = 0 ; i < myObjects.Length ; ++i )
        Debug.Log( myObjects[i].name ) ;
}

Accessing a GameObject that doesnt exist:

Trying to use the result of a getter that's returning null:

Accessing an element of a non-initialized array

private GameObject[] myObjects ; // Uninitialized array

private void Start()
{
    for( int i = 0 ; i < myObjects.Length ; ++i )
        Debug.Log( myObjects[i].name ) ;
}

Less common, but annoying if you don't know it about C# delegates:

Adding another common case that came up in a recent question.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
Loading
added 36 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading
deleted 18 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading
added 11 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading
added 943 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27
Loading