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

How do i fix a NullReferenceException error?

Discussion in 'Scripting' started by ipswitch2, Jun 8, 2010.

  1. ipswitch2

    ipswitch2

    Joined:
    Jun 8, 2010
    Posts:
    7
    Hey, im very new to the system and writing scripts and whatnot. I have been watching videos to try and learn the ropes though. I made a script and when i went to play the game one error came up, it was a Nullreferenceexception error. Ive tried alot to fix it but nothings working. Heres the error...
    NullReferenceException
    Move Around.Update () (at Assets\Move Around.js:19)

    and heres the line the error is on...
    var bullit = Instantiate(bullitPrefab,
    GameObject.Find("spawnPoint").transform.position,
    Quaternion.identity);

    Im stuck, please help me on how to fix this error :)
     
  2. Brucebannor

    Brucebannor

    Joined:
    Jan 22, 2010
    Posts:
    18
    Either make sure you have..

    public var bullitPrefab : GameObject;

    ..and drag the prefab from your project folder onto the variable in the inspector of the GameObject this script is attached to. Or you don't really have a GameObject named "spawnPoint" in your scene.
     
  3. waltermana

    waltermana

    Joined:
    Jun 8, 2010
    Posts:
    172
    assmuming the bullitprefab exists and a spawn point exists you might want to make sure the Vector3 position your getting it compiling in the right order - it might be trying to find the position before it pinpoints the spawn point object
     
  4. ipswitch2

    ipswitch2

    Joined:
    Jun 8, 2010
    Posts:
    7
    I have the bullet prefab if i did this right...
    var bullitPrefab:Transform;

    and its parented with the object i want to move
     
  5. FreakFish

    FreakFish

    Joined:
    Jun 4, 2010
    Posts:
    119
    that looks like the very spelling mistake from the tornadotwins' tutorial on youtube. try writing out the code again from scratch, or just downloading it from their website and editing it to your needs :) i find the best way to learn is to cannibalize scripts and create new frankenstein scripts.
     
    antopia-hk likes this.
  6. ipswitch2

    ipswitch2

    Joined:
    Jun 8, 2010
    Posts:
    7
    haha yeah it is from the tornadotwins, however i dont really even know the basics of writing a script so therefor i dont know how to fix the mistake thats made in this one :p
     
  7. Creative

    Creative

    Joined:
    May 8, 2010
    Posts:
    184
    Do you have an object called spawnPoint and it's located somewhere in the scene?
     
  8. ipswitch2

    ipswitch2

    Joined:
    Jun 8, 2010
    Posts:
    7
    Yeah i set the spawn point to be right in front of the character i made, thats where the shots are supposed to come out of :/
     
  9. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    Well, the answer to why you have the error is pretty simple. GameObject.Find isn't finding your object. I'd suggest tagging it with a specific tag like "Spawn" and then using GameObject.FindWithTag("Spawn"). Sometimes it's hard to get it to properly catch a name, especially if that name contains spaces.
     
  10. Creative

    Creative

    Joined:
    May 8, 2010
    Posts:
    184
    Also u can try finding it with .Contains
     
  11. ipswitch2

    ipswitch2

    Joined:
    Jun 8, 2010
    Posts:
    7
    I tried putting in GameObject.FindWithTag("Spawn") instead of GameObject.Find("spawnPoint") but it still wont take away the NullReferenceException and i cant shoot from the spawn point :/
     
  12. ipswitch2

    ipswitch2

    Joined:
    Jun 8, 2010
    Posts:
    7
    {
    var bullit = Instantiate(bullitPrefab,
    GameObject.Find("Spawn").transform.position,
    Quaternion.identity);
    bullit.rigidbody.AddForce(transform.forward * 2000);
    }

    Theres the bit of script with the error on it...It says the error is at GameObject.Find("Spawn").transform.position but i dont know how to fix it and its really holding me back :/
     
  13. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,571
    You have to tag the object "Spawn" AND use GameObject.FindWithTag, not GameObject.Find.
     
  14. ipswitch2

    ipswitch2

    Joined:
    Jun 8, 2010
    Posts:
    7
    Thanks for all the help! Somehow it started working :) i didnt change anything from this time i tried it to the last time but when i tried shooting this time it worked lol. Thanks again i really appreciate it :)
     
  15. David_29

    David_29

    Joined:
    Jan 22, 2014
    Posts:
    12
    Read it all before you conclude it.

    There is a better answer for handling `NullReferenceException`. If you can relate programming in Java language like me, you can prevent from sending a null error by using the `try-catch` block. Try it for yourself! ;-)

    If you're using in C#, check if you have `using System;` at the top of your script file. If not, add it. Now, you can use all sorts of `Exception` classes while try-catching a line of code.

    If your're using Javascript, use `import System;`

    Here's an example:

    using System; // --> This exact line of code. That's it.
    using UnityEngine;

    public class Test : MonoBehaviour {

    public GameObject player; // --> Example to check if there's a null content;

    public void Update() {

    // You may now catch null reference here.
    try {

    player.transform.Translate(0, 0, 2);

    } catch(NullReferenceException e) { // --> You may use this type of exception class

    }

    }

    }

    Also remember, you can catch also other exceptions such as `MissingReferenceException`, `MissingComponentException`, `IndexOutOfRangeException`, or any other exception classes as long as you include `using System` in your script. That is all.