- Home /
Disabling collider according to color?
I'm trying to make it so that if my player (a ball) rolls over a cube of a different color than the ball it falls through. I thought I'd be able to do this simply by checking if the color of the ball was equal to the color of the cube, but this doesn't seem to be working.
This script is applied to the cube:
#pragma strict
var color:Color=Color.blue;
gameObject.renderer.material.color=color;
function OnTriggerEnter(bink:Collider){
if ((bink.tag=="Player")&&(bink.renderer.material.color != color)){
Disappear(5);
}
}
function Disappear (runTime : float) {
var timer = 0.0;
while (timer < runTime) {
gameObject.collider.enabled = false;
timer += Time.deltaTime;
yield;
}
gameObject.collider.enabled = true;
}
Answer by Spinnernicholas · Dec 31, 2013 at 07:59 PM
Try this:
var color:Color=Color.blue;
gameObject.renderer.material.color=color;
function OnCollisionEnter(col:Collision){
if ((col.collider.tag=="Player")&&(col.collider.renderer.material.color != color)){
StartCoroutine(disableCollisions(col.collider));
}
}
function disableCollisions(bink:Collider)
{
this.collider.enabled = false;
yield WaitForSeconds (2);
this.collider.enabled = true;
}
This will disable the collider for 2 seconds and then re-enable.
Assets/Scripts/objects/colorswitcher/blueColorBlock.js(8,37): BCE0017: The best overload for the method 'blueColorBlock.disableCollisions(UnityEngine.Collider)' is not compatible with the argument list '()'.
Script error: OnCollisionEnter This message parameter has to be of type: Collision
Herm hum...
Answer by IgorAherne · Dec 31, 2013 at 05:51 PM
I don't see a point in using the yield keyword. Get rid of it and you should get a working code, man
yield makes that function an enumerator. So, every time you call Disappear, it will disable the object's collider, and then continue counting time until timer > runTime. Then, it re-enables the collider and resets until you start calling it again.
Yes, but it doesn't seem to be working with or without yield...