0

image

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;

        if(soilSelectScript.wasClicked == true)
        {
            isVisible = false;
        }
    }


    if (isVisible == true)
    {
        OnTriggerEnter();
    }
    else if (isVisible == false)
    {
        OnTriggerExit();
    }
 }



  [SerializeField]
  LayerMask onMask;

  [SerializeField]
  LayerMask offMask;

  void OnTriggerEnter()
  {
      Camera.main.cullingMask = onMask;
   }

   void OnTriggerExit()
   {
       Camera.main.cullingMask = offMask;
   }
}

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 :)

6
  • your title made about as much sense as lathering yourself up with ketchup and throwing yourself in the lion pit.. Your detail, not a lot more, however, why not just use a tile grid? Commented Jul 11, 2024 at 17:59
  • 1
    One thing you can do is put all those GameObjects on their own layer, and then include/exclude just that single layer from your main camera's settings Commented Jul 11, 2024 at 18:29
  • Thank you so much @ipodtouch - I've got that working greatly (I didn't even know you could make layers lol), but now my problem is when I click on one of the boxes, it doesn't trigger them all to disappear again. I'm not sure why it's not working, I tried it on a different object in the game and it worked, so not sure why it's just the squares that don't like it. I've spent about 2 hours trying to get it to work :( any ideas? Commented Jul 15, 2024 at 13:28
  • Are you changing the select boxes' gameobject's layers? or the Camera.cullingMask? Commented Jul 15, 2024 at 13:31
  • I think so, it starts off invisible and it does appear - just making it go again is the problem - to turn the visibility on and off, (still in the Visibility script attached to the empty parent) I've made a LayerMask onMask and the same for an offMask, and when the Slot 1 is clicked, that triggers Camera.main.cullingMask = onMask , and when one of the squares is clicked, it should trigger offMask but it does nothing Commented Jul 15, 2024 at 15:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.