Search Unity

Resolved NullReferenceException Warning for GameObjects

Discussion in 'Scripting' started by ozlgktg, Mar 30, 2020.

  1. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Hello Dear Unity Community,

    There are too many explainings about NullReferenceObjects, but I could not fix it yet.

    I have defined my GameObjects variables as public, and drag&drop Objects to inspector panel like this

    Code (CSharp):
    1.  
    2.  
    3. public class scalescript : MonoBehaviour
    4. {
    5.  
    6.     public int Kontrol;
    7.     public float AnlikBoyutx, AnlikBoyuty, IstenenBoyutx, IstenenBoyuty;
    8.     public Vector3 vector;
    9.     public Vector3 Istenenvector;
    10.     public GameObject nesne;
    11.     public int ZoomSpeed = 8;
    12.  
    13.     public GameObject Buton_Play;
    14.     public GameObject ButtonDown;
    15.     public GameObject ButtonUp;
    16.     public GameObject Canvas_Buton;
    17.  
    18.     void Start()
    19.     {
    20.         Kontrol = 3;
    21.         Canvas_Buton = GameObject.Find("CanvasButon");
    22.         Buton_Play = GameObject.Find("PlayButonu");
    23.         ButtonDown = GameObject.Find("Button_Down");
    24.         ButtonUp = GameObject.Find("Button_Up");
    25.         nesne =  GameObject.Find("Quad_Video5");
    26.         vector = GameObject.Find("Quad_Video5").GetComponent<Transform>().localScale;
    27.      }
    28.  
    This is scalescript code which has NullReferenceException warning:
    Code (CSharp):
    1. public void TargetLost()
    2.     {
    3.         ButtonUp.gameObject.SetActive(false);  //its ok
    4.         ButtonDown.gameObject.SetActive(false); //has NullReferenceException error
    5.  
    6.         nesne.transform.localScale = vector; //Its ok
    7.         GameObject.Find("Quad_Video5").gameObject.GetComponent<VideoPlayer>().Stop(); //Its ok
    8.     }
    9.  
    10.     public void TargetFind()
    11.     {
    12.         Buton_Play.gameObject.SetActive(true); //Its ok
    13.         ButtonUp.gameObject.SetActive(true); //has NullReferenceException error
    14.     }
    This my objects on scene
    Scene.PNG

    This is Inspector Panel QuadVideo5 and ScaleScript
    InspectorPanel.PNG

    This is Error:
    NullReferenceException.PNG

    Why ı see this NullException Warning? Could you help me please...

    Thank you...
     

    Attached Files:

  2. TuckerFlynn

    TuckerFlynn

    Joined:
    May 18, 2015
    Posts:
    10
    It looks to me like you are manually setting the public gameobjects via the Inspector but you're also setting them in the Start method via GameObject.Find. GameObject.Find only returns active objects though, and from the screenshot of your hierarchy it looks like the objects are disabled.

    Removing these lines should fix the error:

    ButtonDown = GameObject.Find("Button_Down");
    ButtonUp = GameObject.Find("Button_Up");

    Or you can start the scene with those objects active and disable them within the script after the references have been set.

    As a side note, if you are setting all the public gameobjects in the inspector there's no need for any of the lines using GameObject.Find. I've seen many posts recommending that you avoid using that method at all if possible. In particular the line:
    vector = GameObject.Find("Quad_Video5").GetComponent<Transform>().localScale;

    should be simplified to:
    vector = nesne.GetComponent<Transform>().localScale;
     
    Last edited: Mar 30, 2020
  3. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Thank you for your answer TuckerFlynn!! Yes theese objects have to be unvisible of passive. I understand that, You advise to me. Active all of them at first. But make passive with scripts. I'll try it.

    But I dont understand that you said "GameObject.Find only returns active objects though."
    Why I can not do any operation with passive object by using Game.Object?

    Thanks for reply...
     
  4. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    By the way. I have catch that;
    Before TargetFind() function. My Inspector screen is like that:
    InspectorPanel.PNG
    But After TargetFind() fucntion is running, My Inspector screen is like that:
    InspectorPanel-None Game Object.PNG
    At the first Gameobjects are there. But when function run, it has gone..
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can do a lot of things with inactive GameObjects, it's just that Find is not one of the things you can do. GameObject.Find is bad to use in general - it's easy to learn and understand, but it's never the best thing to use in any particular situation. This article has a list of other options that are better than GameObject.Find for particular situations.

    In this case, you almost certainly want to (as others have advised) simply remove the GameObject.Find lines and drag-to-assign all of the references instead.
     
    Joe-Censored likes this.
  6. ozlgktg

    ozlgktg

    Joined:
    Jan 15, 2020
    Posts:
    21
    Thank you Star Manta.

    I have deleted these lines:

    ButtonDown = GameObject.Find("Button_Down");
    ButtonUp = GameObject.Find("Button_Up");

    And it works!

    But I want to understand why? Because The PlayButonu is work fine too. And There is public GameObject Buton_Play; code in my script. And I dragged drop to inspector this gameobject too. So Why this line not give an error? But other gameobjects give error? What is the point in this case. I want to learn it. Could you please explain me as simply?

    Thanks.