- Home /
How to know if a collider has a rigid body inside it or not?
In the board game (chess-like) that Im making, on the board, there will be my main gameObject that I control and various collectibles that my main object "swallows".
Each time it swallows a collectible, a new collectible will be spawned in a random position. This random position must be one of the "unoccupied" board boxes (those that don't have a collectible or my main object on them). So, I want to know every moment which squares of the board are occupied and which aren't.
I put an empty game object prefab on each of the board squares. This prefab has a Rigid Body component and a Box Collider attached. Depending on if another rigid body is inside this collider or not, the prefabs tag will change between 2 tags: empty/ full. What is the best way to do this? (control the tag's String)
I am thinking that maybe I could do this somehow using an OnTriggerStay function or a Selection.Contains class function? I dont have experience in using either one of them.
Answer by Matthew A · Jan 09, 2011 at 04:15 PM
I would suggest something like this:
var currentObjectInstance : int = -1;
function OnTriggerEnter(other : Collider) { if (other.gameObject.tag == "Thingy") { this.gameObject.tag = "Full"; currentObjectInstance = other.gameObject.GetInstanceID(); } }
function OnTriggerExit(other : Collider) { if (other.gameObject.tag == "Thingy" && other.gameObject.GetInstanceID() == currentObjectInstance) { this.gameObject.tag = "Empty"; currentObjectInstance = -1; } }
Edit: Adjusted the example. The tag is used so that we know it's the right kind of object (you might have projectiles / particles or whatever that could result in the OnTrigger functions begin called - you can skip the check if not). The instance id is used to make sure it's the same object that's entering / exiting the box (again, if only one object of that tag exists or can be entering / exiting at a time, you could skip the check).
Thanks for answering $$anonymous$$atthew and for taking time to be so specific. So a Thingy tagged object enters the collider: (boardBoxTag=Empty-->OnTriggerEnter-->thingyPresent=true-->boardBoxTag=Full). I am using swipes to move my main object. After each swipe, I was planning on using GameObject.FindWithTag to create an array of objects with tag= Empty. In other words, this array will contain all the empty (at the end of my swipe event) positions on the game board. Then instantiate a collectible in one (random) of these empty board squares. Would I still need use "InstanceID"?
That sounds reasonable. I've changed the code example to reflect better what you said. Really what checks you perform depend on what objects you have that might set off the trigger (e.g. you might not want a Player object to set the box to full, but a collectible should).
Thank you so much $$anonymous$$atthew, I really appreciate your help! Have a nice evening :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613094711im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to convert a Collider into a Spawner? 0 Answers
spawn a ball in a football game 0 Answers
make 2 object not collide? 2 Answers
Which setup should I use for spawning? 0 Answers
on trigger empty 2 Answers