x


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?

more ▼

asked May 09 '11 at 10:33 AM

Justin 6 gravatar image

Justin 6
1 2 2 2

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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.

more ▼

answered May 09 '11 at 10:42 AM

superpig gravatar image

superpig
557 8 9 24

"Then you can use MonoDevelop (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.

May 12 '11 at 12:57 PM Justin 6

Edited and clarified.

May 12 '11 at 01:55 PM superpig
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2486
x1686
x323
x215
x86

asked: May 09 '11 at 10:33 AM

Seen: 1328 times

Last Updated: May 09 '11 at 10:33 AM