Search Unity

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

Discussion in 'Scripting' started by engelcj, Sep 1, 2015.

  1. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    I have several game objects in an array, where I give an instance at certain points, by the time everything works fine. The problem is when I try to move, the consolé gives me this error when it executed: "NullReferenceException: Object reference not set to an instance of an object". My objects move but gives me error, that I have wrong with my code.

    My Code:

    Code (CSharp):
    1. public float x, y, z;
    2.     public GameObject[] Vehiculos;
    3.     public float spawnTime;
    4.     public Transform[] spawnPoints;
    5.  
    6.     GameObject instantiateVehicles;
    7.  
    8.     void Start () {
    9.         InvokeRepeating ("SpawnController", spawnTime, spawnTime);
    10.     }
    11.  
    12.     void FixedUpdate(){
    13.         instantiateVehicles.GetComponent<Rigidbody> ().velocity = new Vector3 (x, y, z);
    14.  
    15.     }
    16.  
    17.     void SpawnController () {
    18.         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    19.         instantiateVehicles = Instantiate (Vehiculos [Random.Range (0,Vehiculos.Length)], spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation) as GameObject;
    20.     }
    21. }
    22.  
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    There are several lines which could cause the exception to be thrown. Please provide some more information about the error (i.e. just post the complete error message).
     
  3. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @Suddoha The error is generated exactly in this line:
    Code (CSharp):
    1. instantiateVehicles.GetComponent<Rigidbody> ().velocity = new Vector3 (x, y, z);
    My objects move with the use of this line, but my console gives me the error "NullReferenceException: Object reference not set to an instance of an object"
     

    Attached Files:

  4. Chromega1231

    Chromega1231

    Joined:
    Jan 25, 2015
    Posts:
    10
    Could be a couple of things, though I'm wondering if this code is actually what you intend.

    So, it seems like 'instantiateVehicles' points to the most recently instantiated vehicle. However, before SpawnController is invoked for the first time (after 'spawnTime' seconds), 'instantiateVehicles' will be null. So, you could fix that by adding a null check above that line (if (instantiateVehicles) {...}).

    The other possibility is that it doesn't have a rigidbody, but the first thing I said seems more likely.


    So, this code spawns a new vehicle, and forcibly sets the velocity of the most recently spawned vehicle every frame. Is that what you had in mind? If you just want to set the starting velocity of your vehicle, you don't need to put that into FixedUpdate, you can set the velocity right after spawning your vehicle. If you do, for some reason, want to slam the velocity of all vehicles every frame (probably a bad idea, you might get some weird collisions), then you'd probably want to maintain a list of spawned vehicles, instead of just remembering the most recently spawned one.
     
  5. cowtrix

    cowtrix

    Joined:
    Oct 23, 2012
    Posts:
    322
    You don't have a RigidBody component on whatever instantiateVehicles is, or instantiateVehicles is null. Don't do calls on the return value of a GetComponent, it's terrible design. GetComponent<T> once, check for null, do work. Also why are you redding out parts of the exception?
     
  6. engelcj

    engelcj

    Joined:
    May 20, 2014
    Posts:
    122
    @Chromega1231 @cowtrix Thank you very much it just check for null. I sorry for my code i am learning.