I’m making a turn based game. I need to make a sphere that when collides with a an object with the tag “Cell” adds it to an array. When the object stops colliding with the cell remove it for the array. Could someone please help me with this. Thanks !
It must be smth like this, although I didn’t try it.
You will atach this to your moving object(your sphere)
public var myArray:Gameobject;
function OnCollisionStay(collision : Collision) {
if(collision.gameObject.tag == "Cell")
myArray.Push(collision.gameObject);
}
function OnCollisionExit(collision : Collision) {
myArray.Pop(collision.gameObject);
}
First, you want to store a bunch of object somewhere, you don’t know the size but you know it’s going to change => List< Transform >. Or GameObject or whatever.
Now, I see two ways. I’m assuming that the sphere is moving and the rest is wall and such.
- Add a script on every object that can be added to the list. When the sphere enter their collider, you add them to a list, from a script attached to the sphere for instance. On exit, you remove it.
- Add a script only on the sphere. As it is the one to move, you can’t receive collision message (or only the hit if it’s a character controller). You should cast an OverlapSphere every x seconds and compare the old list and new list with list.Except from System.Linq. Those in the new but not in the old are added, those in the old but not the new are removed.