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

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 !