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

set active not working

Discussion in 'Scripting' started by kondor, Sep 19, 2017.

  1. kondor

    kondor

    Joined:
    Jun 18, 2013
    Posts:
    48
    HI,

    I wrote a code where in the start function I disabled a gameobject with: setactive(false)
    and In another function I wrote: setactive(true)
    both functions is on a game manager object I mean its not on the inactive object
    I read on another threads that the reason is because of the garbage collector
    but I have reference in the game manager object to that object
    this is the code:
    Code (CSharp):
    1. void Start ()
    2.     {
    3.         ui.SetActive(false);
    4.         scoreText.text = PlayerPrefs.GetInt("Score").ToString();
    5.     }
    6.  
    7. public void displayUI()
    8.     {
    9.         ui.SetActive(true);
    10.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    How are you calling displayUI?

    How is your reference to ui set? Drag and drop inspector?
    Is the SetActive(false) working?
     
  3. kondor

    kondor

    Joined:
    Jun 18, 2013
    Posts:
    48
    the SetActive(false) is working
    and yes I reference with drag and drop
    and i calling the function with get component and it gives me this:
    NullReferenceException: Object reference not set to an instance of an object
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    So you have a reference error on the code that calls displayUI?

    I'll have to see more to understand why it's null, but basically when you go to call displayUI, it's not getting the script that this method is on (You mention a GameManager) which is why displayUI never runs.