I would like my ‘Enemy’ object change color every time the first person controller jumps off of it. For example, the first time the player jumps off of it, it turns green. second time he jumps off of it, it turns blue and so on. I have this program that works on only the first time jumping off of it. I don’t know how to write a script that would let the player jump off of the object several times but get different results each time. I would greatly appreciate if someone could help me.
Script:
var Enemy : GameObject;
function OnTriggerExit(col : Collider)
{
Enemy.renderer.material.color = new Color(0.0,1.0,0.0);
}
You would have to use a counter for the same. increment the counter each time the player jumps off the object then check the counter in a switch case and then apply the colour as per your choice, And yes don’t fogey to reset the counter after your end condition.
I would make a List collection of all the colors I want to cycle through and use an index to choose the current color to display.
When the player triggers the OnTriggerExit method, the index is incremented like so: index++. To keep it event driven and
out of the update, you can also call a method that changes the color to the current List index like so: ChangeColor(index);
In ChangeColor, just tell the object to change to the List[index] color and you are set.
what Ingot has answered is actually a better way of doing what you want to achieve as if there are lot of colours that you want to change then its best you have them listed in a list and then apply them in the manner suggested by Ingot.
Hey guys I figured it out. I should have mentioned before that there aren’t going to be more than 4 colors in the game but yes if there was more than that, I would definitely use Ingot’s code. here is my final script that works:
var Enemy: GameObject;
var ExitCounter : int;
function OnTriggerExit(col : Collider){
if (col.transform.tag == "Player"){
ExitCounter += 1;
if (ExitCounter == 1){
Enemy.renderer.material.color = new Color(0.0,1.0,0.0);
}else if (ExitCounter == 2){
Enemy.renderer.material.color = new Color(0.0,0.0,1.0);
}else if (ExitCounter == 3){
ExitCounter = 0;
Destroy (Enemy.gameObject, 2);
}
}
When the player jumps on it and jumps off it, the object turns green and when he jumps off it again, the object turns blue and when jumped off again, it will disappear in 2 seconds.
Although I would like to use this piece of code for disappearing the object so I can reset the level: transform.gameObject.setActive(false); But it doesnt seem to work, any ideas?
Since the object is already destroyed you won’t be able to set it to false, if you want to make the object disappear then call the setActive(false) on the object before you destroy it.