Unity – how do I use one child gameObject, out of lots, as a trigger to make them all invisible?

  Kiến thức lập trình

I’m currently making a small game, and one mechanic I’m working on is being able press a “plant flower” button, which triggers all the boxes, showing where you can plant it.
Then you click where you want to put it, and that triggers all the boxes to disappear.

I have an empty GameObject, called Soil.
Inside that, I have lots more empty GameObjects, which are the rows.
Inside them I have each individual box. (see image, because it’s confusing to explain :P)
There may be an easier way to do that and still be able to detect a single box, but that can be dealt with later.

My current problem is making the boxes disappear again.
Here is the Visibility script that’s attached to the empty gameObject Soil:

public class Visability : MonoBehaviour
{
    private Select selectScript;
    private Select soilSelectScript;

    public bool isVisible = false;

    //I thought making an array of them all was a good idea, but I'm not sure how what to do with it
    public GameObject[] soilChildren;




    
    void Start()
    {
        
        selectScript = GameObject.Find("Slot 1").GetComponent<Select>();

        soilSelectScript = GameObject.Find("Hoe Select 2").GetComponent<Select>();

    }

     
     void Update()
     {
        // If the "wasClicked" bool = false, make all the squares invisible.
        if (selectScript.wasClicked == false)
        {
            isVisible = false;
        }

        // If "wasClicked" = true, make everything visible.
        else if (selectScript.wasClicked == true)
        {
            isVisible = true;
        }


        // Works perfect up to here, but when I try to make them invisible again, nothing happens.
        // I imagine it's got something to do with it being inside 2 empty GameObjects, unless I'm doing something very stupidly wrong lol
        /*if(soilSelectScript.wasClicked == true)
        {
            isVisible = false;
            
        }*/




        // This stuff works fine
        if(isVisible == false)
        {
            foreach (Renderer r in GetComponentsInChildren<Renderer>())
            {
                r.enabled = false;
            }
        }
        else if (isVisible == true)
        {
            foreach (Renderer r in GetComponentsInChildren<Renderer>())
            {
                r.enabled = true;
            }
        }
     }
 }

I have a Select script on all the boxes (and whatever else can be clicked), which simply detects whether or not the mouse clicked it, and sets a bool wasClicked to true.

I’d just like to find the simplest way of doing this. Any help and code samples are greatly appreciated 🙂

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT