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

Solved NullReferenceExeption

Discussion in 'Scripting' started by jo6x, Feb 25, 2016.

  1. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113
    The script InteractScriptFPSControler should set the FireFXFlameBall01 form the FireFXFlamBallScript to active by using SetActive to true.

    public class InteractScriptFPSControler : MonoBehaviour
    {
    public float interactDistants = 5f;
    bool keyActive119 = false;
    GameObject FireFXFlameBall01 = null;

    void Update ()
    {
    if (Input.GetKeyDown (KeyCode.O))
    {
    Ray ray = new Ray (transform.position, transform.forward);
    RaycastHit hit;
    if (Physics.Raycast (ray, out hit, interactDistants))
    {
    if (hit.collider.CompareTag ("Door119") && keyActive119 == true)
    {
    hit.collider.transform.GetComponent<Door119Script> ().ChangeDoorState ();
    FireFXFlameBall01.SetActive (true);
    }
    }
    }


    public class FireFXFlameBall01 : MonoBehaviour
    {
    void Start ()
    {
    gameObject.SetActive (false);
    }

    The object is behind a door and smoke. Bij opening the door the FireFXFlameBall01 should get active, but I get an Error called:
    NullReferenceException: Objectreference not set to an instance of an object.
    The question is how can I fix this.
     
  2. manlaikin1994

    manlaikin1994

    Joined:
    May 15, 2015
    Posts:
    179
    if(gameObject != null)
    {
    //do sth
    }

    or use try catch
     
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi Jo,

    When showing code in the forums please use Code Tags: http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    It looks like 'FireFXFlameBall01' is empty (it has no reference). So when you try to call SetActive on it, it'll say there's no object to call SetActive on.

    Put the variable up like this

    Code (CSharp):
    1. public GameObject FireFXFlameBall01;
    then attach the correct object to the script in the inspector (by dragging and dropping).
     
  4. jo6x

    jo6x

    Joined:
    Jan 2, 2016
    Posts:
    113

    Thanks both Manlaikin and Nigey for your reply. :)

    I'll tried this option first:
    I changed the GameObject to public, yet I got a new Error wich is called as Unasigned Referance Exeption, this should be the problem that the object is empty.
    How can I drag and drop the object into the script. The script is already assigned to the object (by using Add Component).
    Do I need to do it like the video https://unity3d.com/learn/tutorials...between-components-gameobjects?playlist=17117 or can you tell me how.
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    you're not trying to add the script, but add something into the reference slot in the inspector for the script. Drag whatever you want the reference "FireFXFlameBall01" to refer to into the slot in the inspector
     
  6. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129