- Home /
The question is answered, right answer was accepted
Add a GameObject to a list OnCollisionEnter and remove it from the list OnCollisionExit
Hi, I got severals objects colliding to a cube and I would like to add them to a GameObject[] OnCollisionEnter(); then I want to remove them if they go away.
I tried this for the first part :
var Enemies : GameObject[];
function OnCollisionEnter (other : Collision) {
Enemies(x) = other.gameObject;
}
function OnCollisionExit (other : Collision) {
Enemies(x) = null;
}
as you can see, my problem is that I don't know how to remove the cube that is colliding or not...
Example: Cube 1 is the one that I apply the script, cube 2, 3 and 4 dont hit it,
few seconds later cubes 2 and 4 are colliding the cube 1 but cube 3 is still away,
here I would like to apply damage to cubes 2 and 4,
then cube 2 go away so only cube 4 is collinding cube 1,
(fixed)here is my problem, I don't know how to remove only cube 2 from the list because I don't know how to get the number of a cube in the list, to remove the correct one...(fixed)
the new problem is how to apply damage to all gameObjects in Enemies[]; ???
hope you can understand what I mean...
I just think to this :
function OnCollisionEnter (other : Collision) {
y += 1;
other.gameObject.GetComponent(number).x = y;
Enemies(other.gameObject.GetComponent(number).x) = other.gameObject;
}
function OnCollisionExit (other : Collision) {
Enemies(other.gameObject.GetComponent(number).x) = null;
}
this might be the solution, can't test it right now so the next problem is how to apply damage to all gameObjects in Enemies[]; ???
Answer by Vylax · Dec 16, 2016 at 12:20 PM
found the solution :
for(i : int = 0; i < closeEnemies.Lengt; i++) {
if(closeEnemies(i) != null) {
closeEnemies(i).gameObject.GetComponent(enemy).health -= damage;
}else{
i++;
}