OnCollisionEnter Not Working (SOLVED) -Read 1st Post-


Video of Problem:

Note: This video only shows gameplay, which is where the problem exists. To see more about it, check my other post:
http://forum.unity3d.com/viewtopic.php?p=367562#367562

What you will notice:
Both boxes have a Collider on it, technically the box collider. One has its collider set as a trigger and the script corresponds with it, the other has colliders turned on and is NOT a trigger. Thus the script on this box has OnCollisionEnter instead.

The scripts are exactly the same, the only difference is that one has OnCollisionEnter and the other has OnTriggerEnter.

Ok, this is kind of hard to explain but here it goes. What I’m trying to do, is get an object to harm my player when my player touches that object… PROBLEM IS, that for some weird as #^$@ reason, my script isn’t sending the message “ApplyDamage” to my player script…

(This script grabs the target information, and applies damage, it’s got a bit of 2 scripts in here because even with a simple “Hey I collided with you!” script, it WILL NOT RUN

var target : Transform;
var damage = 1;
private var lastHitTime = 0.0;

// Cache link to player's state management script for later use.
function Start ()
{
	if (!target)
		target = GameObject.FindWithTag("Player").transform;
}

function OnCollisionEnter( col : Collision ) {
//if (Time.time > lastHitTime + 0.25 )
	//{
    if ( col.collider.tag == "Player" )
		{
			target.SendMessage("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
			//lastHitTime = Time.time;
		}
	//}
}

Using just this will not work (This is stripped to the bare essentials.

var target : Transform;
var damage = 1;

// Cache link to player's state management script for later use.
function Start ()
{
	if (!target)
		target = GameObject.FindWithTag("Player").transform;
}

function OnCollisionEnter( col : Collision )
{
			target.SendMessage("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
	
}

The script I’m using to react with? The Lerpz script of “Third Person Status”. I cannot get that code to send a a message. I’ve even changed the target.SendMessage to BroadcastMessage, moved the script to large and small targets, and still no reaction. No sound (I have sound confirmation), no change in values, nothing. The script refuses to send the message when I collide with it.


SOLVED

It took me another 4 hours, but it was finally solved. Turns out I needed “OnControllerColliderHit”, and I needed it slapped into my ThirdPersonWalker Script. Upon doing that, whenever I touch my “Enemy” I take damage… Now I need my character to bounce backwards, and I need my character to NOT take damage when I hop on an enemy (essentially destroying the enemy), but that’s another script.

Hello Teriki Tora!
Could you replace the method OnCollisionEnter of the bomb (or whatever it is) by the following, and tell me what the debug window says?

	function OnCollisionEnter(col: Collision) {
		Debug.Log("Colisión tag: "  + col.transform.root.tag);
		Debug.Log("Colisión nombre: "  + col.transform.root.name);
	}

Thanks…

I’ve tried sending something to the Debug window via a collision and via update, the script refuses to run.

function Update ()
{
//gameObject.BroadcastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
Debug.Log("I'm running!")
}

Did not even work

The entire script won’t run, I have it put on different objects. I’ve even stuck a sound on an object so that when I touched it, it played a sound. For some reason it just won’t let the script run.

Is the script attached to the bomb?.. and if so, is the bomb active at runtime?

Update always is called under those conditions…

The script is attached to… actually technically three objects in my scene that are seen at nearly the entire time (considering it’s a test level and everything’s right there in sight). The script refuses to run irregardless of where I put it (I even put it on my player once, and it still didn’t run, even with the update function)

if a simple Debug.Log("I'm running!") in Update method doesn’t work, i’ve give up because there is another problem i guess.

Just to test, create a box, attach a very simple script like you did, an see what happens. If that works, evidently something affects your bomb object…

:?

I’m at a loss myself, as it is the only script that refuses to run. My Game HUDs (actually it’s one, but I split the script so I had two different depths) will run, my player controller will run, my camera script will run. Every script that I have in my scene will run, but the only one that refuses to run, is the new one that I created that just has a simple update script.

Well, i’m a newbie, so i can’t figure out what is happening; although i think the culling mask settings could be the problem (now that you did a mention of it).

Culling mask only applies to cameras, and only applies to rendered objects.

(Now I’m back to square 1, Update will work, but collision will not X.X)

Yes, applies to cameras, but if a camera doesn’t view an object, it will not be render and the script will not work… but evidently you see that object.

Mmm… i insist (because it’s the only thing that i can do for now) asking you about the active status of the bomb: it is in the scene, but it is active?

Well, it’s dinner time…

See you (i hope with a solution).
Kokumo

Active how? Active as in moving? No, it’s a stationary object. Active as in the script is running? According to the update function on the newer (yet same script) file, it’s actively running. But as soon as I try to get it to react to a collision, it does nothing. (I even have it set to the terrain, and it -STILL- doesn’t give me a message saying where my Player has touched)

Been a few more hours, and I can only narrow it down to “OnCollisionEnter” not even recognizing itself. I can create a simple script (seen below), but no matter what I touch, the script won’t go through the OnCollisionEnter Function. I’ve put rigidbodies, Box Colliders, and other colliders on my objects in order to get them to react, but the OnCollisionEnter part of my scripts won’t run at all.

As seen below, I have my Debug.Log tell me that my function went off. It’ll tell me the “I’m Running!” message, but it won’t tell me the colliding message.

function update()
{
Debug.Log("I'm Running!");
}

function OnCollisionEnter(col : Collision)
{
Debug.Log("I collided with something.");
}

Hi again!
I’m going to start again.

If you put into Update method Debug.Log(“whatever message you want”), it does appear?.. if the answer is “yes”, we are sure that the object is active, so we can continue testing it. → EDIT: your answer was “YES” :stuck_out_tongue:

Second thing to have in mind is to be sure of fulfill the requirements for the execution of OnCollisionEnter (you could use OnTriggerEnter instead).

Check this out: http://unity3d.com/support/documentation/Manual/Physics.html (under “Collision action matrix” title); you will find those requirements.

  • Has your character controller a rigidbody attached?
  • The bomb has attached a rigidbody and a collider, or one of those?
  • if the bomb has a collider… it’s checked as trigger?.. if it does, you will execute OnTriggerEnter instead OnCollisionEnter.

I put a rigidbody on both objects in question, tripled the colliders (actually I tried all three of the different colliders at different times). And the colliders were not triggers.

AND IT STILL refuses to detect collision.

OnTriggerEnter works fine, but OnCollisionEnter will not work. I don’t want a trigger, because I need to go in and out of it in order for it to activate it. I want it to go off on touch.

As soon as I change OnTriggerEnter to be OnCollisionEnter (And changing the collider to not be a trigger), the code refuses to work.

A trigger (in this case) it’s quite similar to the collision, so you can use it as a workaroud.

Mmm… i don’t know what else i can tell you, because i did a test and it works.
I put an empty GameObject with a particle system nested, and then i attached a sphere collider to the empty GameObject.
The second thing i did was attach a very simple script with an OnCollisionEnter method, which prints on debug’s window “Hello world”.
When i hit that object with my character controller, works fine
:?

See, that’s what I don’t get, it refuses to detect the collision… It’s frustrating the heck out of me because every other function that I have works fine. OnCollisionEnter refuses to work.

I’m working on uploading a video of the problem. Give me about 3 hours of compression and upload time (combined) to get it up and viewable. You will only see the game window but that’s how fraps works (curse you fraps) However, I will guarantee that these two boxes (in the video) are exact duplicates, except for 2 variables. OnCollisionEnter and OnTriggerEnter, and IsTrigger True and False for the collider.

Upload started as of —2:30PM EST—

Video’s up:

That was faster than I expected, well anyways, I have the video up. Most of the information is in the description except for this one little thing. This is the code that I’m trying to get to work in-game, but the problem is the OnCollisionEnter function. The only difference between the two boxes, is that OnCollisionEnter is switched with OnTriggerEnter (And the trigger box has its Box Collider set as a trigger). Oh yeah, and what it tells me when the function activates.

var target : Transform;
var damage = 1;
private var lastHitTime = 0.0; 

function Start ()
{
   if (!target)
      target = GameObject.FindWithTag("Player").transform;
} 

function Update () {
Debug.Log("I'm Running! (Collision)");
}

function OnCollisionEnter(){
Debug.Log("I Collided");
if (Time.time > lastHitTime + 0.25 )
   {
		target.SendMessage("ApplyDamage", damage,SendMessageOptions.DontRequireReceiver);
         lastHitTime = Time.time;
   }
}

Hi again!..

I found this at manual reference:

What about that?

A Rigidbody is attached to both boxes, and as a default “Is Kinematic” is not checked. That’s the effect that I have. (video) Same problem.

Now thtat i’m checking, in the test i did, neither my character controller nor my empy gameObject has a rigidBody attached… and works fine.

Anyone else can help with this issue?