Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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,186
    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,186
    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.