I’m not quite sure why this isn’t changing the color of the light component.
var color0 = Color.red;
var color1 = Color.green;
var color2 = Color.blue;
function Update () {
if (Input.GetKey ("space")) {
light.color = color0;
}
else {
light.color = color1;
}
}
function OnCollisionEnter(collision : Collision) {
print("Hit");
light.color = color2;
}
The part about pressing space to turn the light color to red and back to green works.
On collision, it does print ‘Hit’, but the color doesn’t change to blue.
I’ve attached a screenshot of the game object/components/etc. (Sorry if it stretches your screen a bit, shrinking it made it rather unreadable).
the if-statement in the update function is causing trouble. if you hold space the light will turn red, but in any other case the light will be green. what happens is that the collision stuff works perfectly, but in the next update call the color is changed back to green.
Thanks, after separating out into functions and setting a hit variable in the collision detection, I was able to add more ifs to that section and get all 3 working. Here’s what I did in case anyone else ever runs into this.
var color0 = Color.red;
var color1 = Color.green;
var color2 = Color.blue;
var hit = false;
function setColor0(){
light.color = color0;
}
function setColor1(){
light.color = color1;
}
function setColor2(){
light.color = color2;
}
function OnCollisionEnter(collision : Collision) {
hit = true;
}
function OnCollisionExit(collision : Collision) {
hit = false;
}
function Update () {
[More Code]
if (Input.GetKey ("space")) {
rigidbody.velocity.y = 0;
rigidbody.velocity.x = 0;
setColor0();
}
else if (hit == true){
setColor2();
}
else{
setColor1();
}
}