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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

"Object reference not set to an instance of an object"

Discussion in 'Scripting' started by Grillstern, Mar 23, 2015.

  1. Grillstern

    Grillstern

    Joined:
    Oct 14, 2013
    Posts:
    32
    Hi,

    unfortunately I got the following error msg I can't handle at the moment:

    "NullReferenceException: Object reference not set to an instance of an object
    (wrapper stelemref) object:stelemref (object,intptr,object)
    ParticleManager.Start () (at Assets/Scripts/ParticleManager.cs:20)"



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ParticleManager : MonoBehaviour {
    5.  
    6.     public GameObject particle;
    7.     public int maxParticles = 1000;
    8.  
    9.     GameObject[] particleList;
    10.  
    11.     int nextFreeParticleIdx = 0;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         for (int i = 0; i < 1000; i++) {
    16.             GameObject newParticle;
    17.             newParticle = Instantiate (particle, transform.position, transform.rotation) as GameObject;
    18.             newParticle.GetComponent<Particle>().SetEnabled(false);
    19.             newParticle.transform.LookAt(new Vector3(0,0,0));
    20.             particleList[i] = newParticle;
    21.             Debug.Log("Loading Particle: " + i);
    22.         }
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {  
    27.     }
    28. }
    29.  
    this is the line causing the error, but i don't get why!?

    Code (CSharp):
    1.  
    2. particleList[i] = newParticle;
    3.  
    ...if i remove this single line, Unity will create 1000 particles without any problem, but I want to store them (after instancing) in an array....which isn't working.

    Anyone can help me?
     
  2. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Is it possible that particleList is not initialised?

    Try particleList = new GameObject[1000];
     
    liortal likes this.
  3. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    You never initialize the array. An array expects to be initialized with a predefined size. If you have a collection of variable size, and want to fill it during runtime, use a List<T>, as that can be filled during runtime without needing a set size. Just make sure to initialize it before using it: List<T> list = new List<T>;

    https://msdn.microsoft.com/en-us/library/4kf43ys3(v=vs.110).aspx
     
    liortal likes this.
  4. Grillstern

    Grillstern

    Joined:
    Oct 14, 2013
    Posts:
    32
    ...just for testing purposes: This solution seems to work (for now :) )! Thx!

    But will try Timelog's solution next.
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    I want to add that if you do have a fixed amount of items, like your maxParticles like you have, just use:
    Code (CSharp):
    1. particleList = new GameObject[maxParticles];
    and also make sure to change your for loop:
    Code (CSharp):
    1. for (int i = 0; i < maxParticles; i++)
    This is slightly more performance friendly when you do know the exact maximum size before initializing. In all other cases use a List<T> as a dynamic sized array is less performance friendly.