Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by unbootabru, Jul 31, 2014.

  1. unbootabru

    unbootabru

    Joined:
    Jul 21, 2014
    Posts:
    6
    I'm working on creating a mirror in a 2D environment without Unity Pro by creating a new 'mirrored' object, stripping the 'mirrored' object down to just the sprite, and rendering it in the right position. (as suggested here: http://gamedev.stackexchange.com/questions/24770/can-i-make-a-mirror-with-unity-free)To keep track of objects in front of the mirror I am trying to add them to a list. However, I am getting this error every time an object crosses the mirror plane.

    NullReferenceException: Object reference not set to an instance of an object
    mirror.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/mirror.cs:26)

    Here is my C# code: (sorry I do not know how to use code tags)

    public class mirror : worldObject {

    private List<GameObject> MirroredObjects;

    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void OnTriggerEnter2D(Collider2D coll) {
    Debug.Log (coll.gameObject); <-- works fine and looks like coll.gameObject is indeed correct
    MirroredObjects.Add (coll.gameObject); <---- line throwing the error

    }
    }
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    I suspect that MirroredObjects is the thing that is null.

    Try changing the following:
    Code (csharp):
    1. private List<GameObject> MirroredObjects;
    to:
    Code (csharp):
    1. private List<GameObject> MirroredObjects = new List<GameObject>();
     
    Polymorphik likes this.
  3. unbootabru

    unbootabru

    Joined:
    Jul 21, 2014
    Posts:
    6
    SOLVED. You're the man(or woman). For solution and code-tags.