- Home /
Disabling objects from Collision detection
My problem has to do with lights.
I have a spotlight, and I would like the spotlight to be turned off when my character touches an object I have created.
The script I have is:
private var GotHit = false; var TargetLight : Light;
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.tag == "Collide")
{
GotHit = true;
}
}
function Update () { if(GotHit) { TargetLight.enabled = false; }
}
For some reason, It doesn't work. I've checked my tags, and I've specified the object to be disabled.
Does anyone have a general script to be used on collision to disable another object?
Answer by superpig · May 09, 2011 at 10:42 AM
Why are you splitting the logic across both functions? Why not just do:
function OnControllerColliderHit(hit: ControllerColliderHit) {
if(hit.gameObject.tag == "Collide") TargetLight.enabled = false;
}
If you do it that way, then you're reducing the number of things that could be going wrong.
Once you have an implementation that is simple like this, you can start debugging it. The first thing you could try would be to add a Debug.Log command, like this:
function OnControllerColliderHit(hit: ControllerColliderHit) {
Debug.Log("OnControllerColliderHit is executing!");
if(hit.gameObject.tag == "Collide") TargetLight.enabled = false;
}
When you run that script, you should see the message "OnControllerColliderHit is executing!" whenever you collide with anything. If you do, then we know that the problem is with the way that you're responding to collisions; while if you don't, then we know that the problem is that you're not being informed about collisions at all. That will help us narrow down the source of the problem.
"Then you can use $$anonymous$$onoDevelop (or just Debug.Log) to make sure firstly that OnControllerColliderHit is being called, and secondly that hit.gameObject.tag is what you think it is."
I need you to simplify your meaning please, I'm not sure what to do. The coding you gave was my first script, but it wasn't working so I had changed it to what is above. So it's not a scripting problem, but the order of functions that are called? I'm not sure.