Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Null Reference Exception

Discussion in 'Scripting' started by Jsmucker, Dec 9, 2014.

  1. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    For some reason, I keep getting this error. The specific area of code looks like this:
    Code (CSharp):
    1. GameObject remains;
    2. public virtual void Die()
    3.     {
    4.  
    5.         if (!enabled || !vp_Utility.IsActive(gameObject))
    6.             return;
    7.  
    8.         if (m_Audio != null)
    9.         {
    10.             m_Audio.pitch = Time.timeScale;
    11.             m_Audio.PlayOneShot(DeathSound);
    12.         }
    13.  
    14.         RemoveBulletHoles();
    15.         Instantiate(remains, transform.position, transform.rotation);  // This is where the error is
    16. Destroy(gameObject);
    17.         foreach (GameObject o in DeathSpawnObjects)
    18.         {
    19.             if(o != null)
    20.                 vp_Utility.Instantiate(o, transform.position, transform.rotation);
    21.         }
    22.  
    23.     }
     
    Last edited: Dec 9, 2014
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Line 16 should clearly be at the end, as it destroys the game object in which this component is.

    And if you are looking for help, you should post the exact error message and especially mention which line of the posted code is responsible for it.
     
  3. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Thank you, that fixed a problem I had. Fixed, but I am still getting the error. The error is occurring at line 17. The error says exactly this:
    NullReferenceException
    UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot)
    UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation)
    vp_DamageHandler.Die () (at Assets/vp_DamageHandlerModified.cs:161)
    UnityEngine.Component:SendMessage(String)
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Destroy doesn't occur until the end of the Update cycle but before rendering.

    @Jsmucker have you initialized DeathSpawnObjects?
     
  5. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    I have, what do you mean by "Destroy doesn't occur until the end of the Update cycle but before rendering."? Also, all the code works when I don't have line 15 in it without returning errors.
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Even though you called Destroy on line 16, the GameObject won't actually be destroyed until the end of the current frame.

    Have you assigned a value to remains?
     
  7. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Yes, I have Gameobject Remains in line 1
     
  8. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    But you haven't assigned it to anything, it is currently null (hence the null reference exception).
     
  9. Jsmucker

    Jsmucker

    Joined:
    Sep 5, 2014
    Posts:
    48
    Ahh, ok. So how would I fix this?
     
  10. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You need to assign it to whatever you are trying to instantiate (usually a prefab). A common way to do that is to make it public and assign it via the inspector.