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

setactive does nothing

Discussion in 'Scripting' started by unity_23shend, Jan 9, 2020.

  1. unity_23shend

    unity_23shend

    Joined:
    Sep 24, 2019
    Posts:
    3
    im new to unity
    trying to disable a canvas when the player touches a collider but it doesnt work
    Code (CSharp):
    1.  public Canvas canvas;
    2.     void Update()
    3.     {
    4.         void OnTriggerEnter2D(Collider2D col)
    5.         {
    6.             if (col.tag == "Player")
    7.             {
    8.      
    9.                 canvas.gameObject.SetActive(true);
    10.             }
    11.             else
    12.             {
    13.              
    14.                 canvas.gameObject.SetActive(false);
    15.             }
    16.         }
    17.     }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Start by putting a Debug.Log before line 6 here, see if OnTriggerEnter2D is being called at all.
     
  3. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    You've got your OnTriggerEnter2D inside the Update() method...
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Well, I guess the good news is that now local functions are allowed in Unity :)
     
  5. unity_23shend

    unity_23shend

    Joined:
    Sep 24, 2019
    Posts:
    3
    it gets printed if it's not inside the update function but it doesn't show the canvas
     
    Last edited: Jan 10, 2020
  6. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    Code (csharp):
    1. void OnTriggerEnter2D(Collider2D col)
    2. {
    3.             Debug.Log("Is player entering? : " + (col.tag == "Player"));
    4.             if (col.tag == "Player")
    5.             {
    6.                   canvas.gameObject.SetActive(true);
    7.             }
    8. }
    9.  
    10. void OnTriggerExit2D(Collider2D col)
    11. {
    12.             Debug.Log("Is player exiting? : " + (col.tag == "Player"));
    13.             if (col.tag == "Player")
    14.             {
    15.                   canvas.gameObject.SetActive(true);
    16.             }
    17. }
    You're using OnTrigger... so make sure you're collider is set as a trigger.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    And make sure the object's tag is correct
     
  8. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    If(col.gameObject.tag =="Player")

    Make sure to add gameObject before tag, so you can get the tag of the game object that the collider is attached to.
     
  9. unity_23shend

    unity_23shend

    Joined:
    Sep 24, 2019
    Posts:
    3
    it works now thank you