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

NullReferenceException Error by Instantiating

Discussion in 'Scripting' started by Xman236, Aug 9, 2018.

  1. Xman236

    Xman236

    Joined:
    Mar 5, 2018
    Posts:
    11
    Hi all,

    Code (CSharp):
    1. public class Particle : MonoBehaviour
    2. {
    3.  
    4.     public double[] position;
    5.  
    6.     public Particle(double[] pos)
    7.     {
    8.         this.position = new double[pos.Length];
    9.         pos.CopyTo(this.position, 0);
    10.  
    11.     }
    12. }
    13.  
    14. public class test1 : MonoBehaviour
    15. {
    16.  
    17.  
    18.     public Particle particle;
    19.     public Particle clone;
    20.     void Start()
    21.     {
    22.  
    23.         double[] tmp = new double[] { 0, 0, 0 };
    24.         particle = new Particle(tmp);
    25.         clone = (Particle)Instantiate(particle, this.transform.position, this.transform.rotation) ;
    26.        
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         Debug.Log(clone.position);
    32.     }
    So I am trying to instantiate a sphere to use it as my Particle class. However when I am trying to read the position of the instantiated clone. It seems that the particle in the Instantiate() is equal to null. So the NullReference error occurs. But why? particle is created in the previous line with new Particle.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please use Debug.Log or run-time debugging to determine what variable is null. What is the exact error?
     
  3. Xman236

    Xman236

    Joined:
    Mar 5, 2018
    Posts:
    11
    The error says that particle from line 24 is null. But to me it doesnt make sense, since I created with new Particle...
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please confirm with Debug.Log. It may have failed the instantiation. I would also put a debug statement inside of the Particle constructor before CopyTo, etc.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    You can't new up a Particle because it is a Monobehavior. You need to create a GameObject first, then use .AddComponent<Particle>() to add it to that GameObject.

    If you want to add it to the same GameObject that test1 is on, just use the this.gameObject property to call AddComponent and get back your reference to a Particle.

    In any case, at that point it would already BE instantiated and not need further instantiation, unless you wanted to make more copies of it.

    ALSO, I recommend not using the word Particle for your class: it is already a sealed class in Unity3D so you are going to confuse yourself and future people:

    https://docs.unity3d.com/ScriptReference/ParticleSystem.Particle.html