c# – show gameobject after click gameobject-ThrowExceptions

Exception or error:

i’m developing android Games with unity i have a problem i have cube 1, cube 2 and cube 3, and also i have button 1, button 2, button 3
i want when i click cube 1 button 1 show
when i click cube 2 button 2 show button 1 hide
when i click cube 3 button 3 show button 2 hide
what i can do

private void Update()
{

    if (Input.GetMouseButtonDown(0))
    {

    }
    RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 100.0f))
        {
            if (hit.transform != null)
            {

                PrintName(hit.transform.gameObject);

        }


    }
}

private void PrintName(GameObject go)
{

    print(go.name);

}

this code only print name of game object i tried to change it not working

How to solve:

An example what you could do is:


public Button button1, button2, button3;

private void PrintName(GameObject cube) {
    Debug.Log(cube.name);
    if(cube.name.Equals("cube1") {
        button1.gameObject.SetActive(true);
        button2.gameObject.SetActive(false);
        button3.gameObject.SetActive(false);
    }
    if(cube.name.Equals("cube2") {
        button1.gameObject.SetActive(false);
        button2.gameObject.SetActive(true);
        button3.gameObject.SetActive(false);
    }
    if(cube.name.Equals("cube3") {
        button1.gameObject.SetActive(false);
        button2.gameObject.SetActive(false);
        button3.gameObject.SetActive(true);
    }
}

You need all buttons as Attributes for that. The buttons would just be the default ui buttons of unity.

Leave a Reply

Your email address will not be published. Required fields are marked *