Search Unity

Button SetActive

Discussion in 'Getting Started' started by henry_arasaki, Jun 6, 2020.

  1. henry_arasaki

    henry_arasaki

    Joined:
    May 26, 2020
    Posts:
    2
    I'm trying to make a button that has SetActive=false when an object A collides with an object B but i'm can't figure out how.
     
  2. henry_arasaki

    henry_arasaki

    Joined:
    May 26, 2020
    Posts:
    2
    public UnityEngine.UI.Button F;

    void Start()
    {

    F = GetComponent<UnityEngine.UI.Button>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.CompareTag("Wall"))
    {
    F.gameObject.SetActive(false);
    }
    }


    This is not working
     
  3. BungJon

    BungJon

    Joined:
    May 17, 2019
    Posts:
    1
    If this is a script for a component on the Button object, try:

    // public UnityEngine.UI.Button F;

    void Start()
    {

    // F = GetComponent<UnityEngine.UI.Button>();
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.CompareTag("Wall"))
    {
    // F.gameObject.SetActive(false);
    gameObject.SetActive(false);
    }
    }
     
  4. Piduoduo

    Piduoduo

    Joined:
    Feb 9, 2023
    Posts:
    1
    can you explain why this works?i think there isn't much difference between F.gameObject.SetActive(false); and gameObject.SetActive(false); (the difference is that the first one can't be used but the second one can be used .it is really amazing!)Thank you !