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

Panel GameObject not activating

Discussion in 'Scripting' started by JoshiiWoshi, May 5, 2020.

  1. JoshiiWoshi

    JoshiiWoshi

    Joined:
    Apr 24, 2020
    Posts:
    5
    I want to enable my panel gameobject when I interact with something but it doesn't get enabled when I tell it to do so. I don't see any errors with my code, I referenced it in the inspector and the console doesn't show any errors. I'm calling the SetDetailText function in another script.

    And what's weird is that the text does get updated AFTER I stop the game.

    Here is my code:
    Code (CSharp):
    1. public class GameManager : MonoBehaviour
    2. {
    3.     //Objects
    4.     public GameObject statusPanel;
    5.     public Text statusText;
    6.  
    7.     //Private
    8.     float textCooldownTime;
    9.  
    10.     void Start()
    11.     {
    12.         statusPanel.SetActive(false);
    13.         textCooldownTime = 2.0f;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (statusPanel.activeSelf)
    19.         {
    20.             textCooldownTime -= Time.deltaTime;
    21.             if (textCooldownTime < 0)
    22.             {
    23.                 statusPanel.SetActive(false);
    24.                 textCooldownTime = 2.0f;
    25.             }
    26.         }
    27.     }
    28.  
    29.     public void SetDetailText(string details)
    30.     {
    31.         statusPanel.SetActive(true);
    32.         statusText.text = details;
    33.     }
    34. }
    35.  
    36.  
     
  2. Bisoncraft

    Bisoncraft

    Joined:
    Feb 5, 2016
    Posts:
    37
    Have you checked the reference in your GameManager script? What I mean is that in your inspector for this script, you have a reference to the status panel. If you click on it, it should point you directly to this statusPanel object - so maybe it could be another object.

    Another question : have you checked in the hierarchy if the object is enabled? Might it be hidden behind another UI object?
     
  3. JoshiiWoshi

    JoshiiWoshi

    Joined:
    Apr 24, 2020
    Posts:
    5
    Yes it does point to the object that I want it to enable.

    The object is enabled in the hierarchy and it is a child of my HUD canvas but the parent is always enabled so there's no chance that the parent is disabled somehow because that would result in disabling every UI objects in my scene and that's not happening.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Is the statusText Text component enabled? (The checkbox in the top left of the inspector for that individual component)
     
  5. JoshiiWoshi

    JoshiiWoshi

    Joined:
    Apr 24, 2020
    Posts:
    5
    Yes it is enabled.
     
  6. JoshiiWoshi

    JoshiiWoshi

    Joined:
    Apr 24, 2020
    Posts:
    5
    I've figured out the problem. It's because the object that I was referencing to from my Player script was the PREFAB of the GameManager object. When I set it to reference the object in the hierarchy, it worked. Is there no way to make it work even when referencing to the the prefab?
     
  7. Bisoncraft

    Bisoncraft

    Joined:
    Feb 5, 2016
    Posts:
    37
    I don't think so. Why would you want to change the object by referencing the prefab?