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

Enable/Disable Canvas

Discussion in 'Scripting' started by iEntity, Mar 21, 2015.

  1. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    So in my game that I am making, you spawn in on 1 of two teams. Each team has a name and the name of the team is hovering over the player.

    Now to get this effect, I have an idea in my mind of what I need to do, but i just cant seem to pull it off.

    So first I will start out with my two canvases (worldspace) disabled, then in my script I have:
    public Canvas team1;
    public Canvas team2;
    And then I put my canvases in them.
    Then I have:
    bool team1Enabled = false;
    bool team2Enabled = false;
    So then I have something like:
    Canvas t1 = myPlayerGo.transform.GetComponentInChildren<Team1>();
    Canvas t2 = myPlayerGo.transform.GetComponentInChildren<Team2>();
    (p.s. the canvases are attached to my player game object)

    Then I have:
    if (team1Enabled =true)
    {
    t1.SetActive(true);
    }

    if (team2Enabled =true)
    {
    t2.SetActive(true);
    }

    Then I have my code that randomizes the players team and that works great..
    So finally I put:
    if(teamID == 1)
    {
    team1Enabled = true;
    }

    if(teamID == 2)
    {
    team2Enabled = true;
    }

    Thank you and please correct me where I am wrong!


    -Entity
     
  2. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hi iEntity,

    Please use ['code'] ['/code'] tags when posting code. :) (without the quotes - then your code will be formatted like the code below, making it much easier to read)

    As for your question -

    You need to call SetActive(...) on the game object, not the component (in this case, the canvas).

    So try this (it takes the canvas components t1 and t2 and then calls SetActive(...) on the game object that owns them):

    Code (csharp):
    1.  
    2. t1.gameObject.SetActive(true);
    3.  
    and
    Code (csharp):
    1.  
    2. t2.gameObject.SetActive(true);
    3.  
     
  3. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hmm, I also think you maybe wanted to assign the canvases into the variables team1 and team2 (not the new variables t1 and t2) maybe?

    Ah, and "=" assigns a value to a variable. "==" checks for equality

    eg;

    if (team1Enabled = true) // assigns true to team1Enabled
    if (team1Enabled == true) // checks to see if team1Enabled is equal to true

    With those changes, the code that you provided would be modified to look like this:

    Code (CSharp):
    1. public Canvas team1;
    2. public Canvas team2;
    3.  
    4. bool team1Enabled = false;
    5. bool team2Enabled = false;
    6.  
    7. team1 = myPlayerGo.transform.GetComponentInChildren<Team1>();
    8. team2 = myPlayerGo.transform.GetComponentInChildren<Team2>();
    9.  
    10. if (team1Enabled == true)
    11. {
    12.    team1.gameObject.SetActive(true);
    13. }
    14.  
    15. if (team2Enabled == true)
    16. {
    17.    team2.gameObject.SetActive(true);
    18. }
    19.  
    20. if(teamID == 1)
    21. {
    22.    team1Enabled = true;
    23. }
    24.  
    25. if(teamID == 2)
    26. {
    27.    team2Enabled = true;
    28. }
    29.  
    Hope this helps.
     
    Last edited: Mar 21, 2015
  4. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    Thank you but when I try to getcomponentinchildren<team1> it says that it can not be found. The canvas is there and it is definitely a child of myplayergo... I do not know what is going wrong here!!

    Thanks
    -Entity
     
  5. iEntity

    iEntity

    Joined:
    Feb 4, 2015
    Posts:
    23
    Maybe we could enable it by using those canvas variables at the top.. ?
     
  6. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hi iEntity,

    I've been experiencing this issue as well - at least with Unity 5.

    I have been working around the issue at the moment by iterating over a game object's "children" and checking each child's name.

    Its inefficient, but it makes things work - for now.

    I'm not checking my code so it could be a little off, but it should be enough to point you in a direction where you can get access to the scripts on those game objects.

    Code (CSharp):
    1.  
    2. for (int i = 0; i < myPlayerGo.transform.childCount; ++i)
    3. {
    4.    gameObject goChild = myPlayerGo.transform.GetChild(i);
    5.  
    6.    // replace team1GameObject with the name of the game object that has the team 1 script on it here
    7.    if (goChild.name == "team1GameObject")
    8.    {
    9.       team1 = goChild.GetComponent<Team1>();
    10.       // do stuff...
    11.    }
    12. }
    13.