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

Ui system

Discussion in 'UGUI & TextMesh Pro' started by HannibleJester, Jun 13, 2021.

  1. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    you can ignore this one and go to one below----Im trying to make a ui that follows npcs and i got that far however i cant figure out how to make it stop calling null references when an npc isnt selected here is the code that keeps yelling null reference
    Code (CSharp):
    1. public SinglePlayer sP;
    2.     public GameObject dBox;
    3.     public GameObject selected;
    4.  
    5.     public void Update()
    6.     {
    7.         selected = sP.selectedNPC.gameObject;
    8.         dBox = selected.transform.Find("DialogueBox").gameObject;
    9.         bool inRange = sP.inRange;
    10.         if(inRange == false)
    11.         {
    12.             dBox.SetActive(false);
    13.         }else if(inRange == true)
    14.         {
    15.             dBox.SetActive(true);
    16.         }
    17.     }
    the selected is calling the null reference but i just want it to only look for an object when it needs to not every time right now it spams null reference even when an object is inside of it so im confused everything else works tho
     
    Last edited: Jun 13, 2021
  2. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    i updated it to this i got rid of null reference but now it chooses every single DialogueBox in the game and makes them active i only want the one that is selected to be active
    Code (CSharp):
    1. public SinglePlayer sP;
    2.     public GameObject dBox;
    3.     public GameObject selected;
    4.  
    5.     public void Update()
    6.     {
    7.         bool inRange = sP.inRange;
    8.         if(inRange == true)
    9.         {
    10.             selected = sP.selectedNPC.gameObject;
    11.             dBox = selected.transform.Find("DialogueBox").gameObject;
    12.             dBox.SetActive(true);
    13.         }else
    14.         {
    15.             dBox = this.transform.Find("DialogueBox").gameObject;
    16.             dBox.SetActive(false);
    17.         }
    18.      
    19.     }
     
  3. HannibleJester

    HannibleJester

    Joined:
    Jan 15, 2021
    Posts:
    9
    Nah Nevermnd i figured it out lol
     
  4. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    666
    You don't seem to be using UI Toolkit but rather UGUI, but also you say you figured it out - would you like us to delete this? Or move it to the proper Forum?