Hi,
I need some help with this script. I want the gameobject to change color either to red,green,yellow when the character enters the trigger and if the color is correct when exit it will activate a gameobject.
My Code so far:
var activeObjects : GameObject;
function Start(){
activeObjects.SetActiveRecursively(false);
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == “Player”)
{
// Script needed. When character enters trigger it will change the material color either red,green,yellow.
// I have this part of the code to change to a single color:
//renderer.material.color = Color red; But I dont no how to change it between red,green,yellow each time the trigger is entered.
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.tag == “Player”)
// Script needed. If renderer.material.color = Color red;
activeObjects.SetActiveRecursively(true);
{
}
}
Here is a simple code to cycle material colors on click that must do the trick. Just fit it to your OnTrigger events.
To (re)enable on red simple check current value of curColor variable.
private var colors: Color[] = [Color.red, Color.green, Color.yellow];
private var curColor: int = 0;
function OnMouseDown()
{
renderer.material.color = colors[curColor % colors.Length];
curColor++;
}
Thanks for the reply. I have the changing of the material working fine "every time I enter the trigger it will change the colour. I have trouble with finding out how to check the current colour to activate the the gameobject. I’m still new to scripting i seem to be able to read it but cant write it very well.
Current script:
private var colors: Color[ ] = [Color.red, Color.green, Color.yellow];
private var curColor: int = 0;
var activeObjects : GameObject;
function Start(){
activeObjects.SetActiveRecursively(false);
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == “Player”)
{
renderer.material.color = colors[curColor % colors.Length];
curColor++;
ActivateDeactivate();
}
}
function ActivateDeactivate(){
if(curColor == [Color.red])
{
activeObjects.SetActiveRecursively(true);
Debug.Log(“Activate”);
}
if(curColor == [Color.green, Color.yellow])
{
activeObjects.SetActiveRecursively(false);
Debug.Log(“Deactivate”);
}
}
You are checking curColor as a Color but it is an index for the color. So you must check curColor == 0:
private var colors: Color[] = [Color.red, Color.green, Color.yellow];
private var curColor: int = 0;
var activeObjects : GameObject;
function Start()
{
activeObjects.SetActiveRecursively(false);
}
function OnTriggerEnter(other : Collider)
{
if (other.gameObject.tag == "Player")
{
renderer.material.color = colors[curColor % colors.Length];
activeObjects.SetActiveRecursively(curColor % colors.Length == 0);
curColor++;
}
}
[ code ] [ /code ] tags (without spaces) really helps make code you paste in readable…there’s a button on the advanced reply/edit, but you have to put them in manually on the quick reply.