OnCollisionEnter doesn't work

Hello, i am almost new to coding and i want to make a simple score system. When CharacterController collided with an object, that object will be destroyed and i will add 1 point to total score, but i doesn’t work… I read a lot of posts about similar problems but never get the answer… Help me please, thank you. This is first script

 public GameObject Dew;
	
	void OnCollisionEnter( Collision other )
	{
		if( other.gameObject.name == "Dew" )
		{
			scoreKeeper.score += 1;
			Destroy (Dew);
		}
	}
`
and this is second script   
  

 
	public static int score;
	
	void OnGUI()
	{
		{
			GUI.Label (new Rect (10, 10, 100, 20), "Score: " + score);	
			
		}
	} 
`

Firstly check the dumb things like

-Is the dew game object named correctly?
-Have you got a problem with collision layers?

Pretty sure Destroy(Dew) doesn’t make sense as Dew isn’t defined in that class, you will need to go Destroy (other.gameObject) because it’s all ready been cast at the start of the function (might be wrong about this).

You seem to be assuming that your variable other is holding the data of the object that is being collided with. In reality, here, “other” is of the unity type “Collision”, and your Collision object holds the information of the collision itself, not the collider.

Change the line:

if( other.gameObject.name == "Dew" )

to:

if( other.collider.gameObject.name == "Dew" )

This will effectively point to 'the name of the gameObject on which we have the collider that is being collided with in our collision (here named “other”)*.

I would also change the name of your variable ‘other’ to something more descriptive, such as “collision”.

Refer to: Unity - Scripting API: Collision

finally change the line Destroy(Dew), to Destroy(other.collider.gameObject), or Destroy(collision.collider.gameObject) if you performed my name change.

Since Dew is not a named variable (as in: you have not written the line: Dew = something anywhere in your script) your current code will raise an error.

Happy coding! :slight_smile:

The problem is that you’ve set the script onto the object as to which you’re testing collision. You need to set the script on the object that’s making the collision. For instance, if you have a player character that when they collide with a box, they get a point, you need to have the script written as such:

Script on the box example:
if(collision.gameObject.name == "player")
{
     //do stuff here like destroy box and add point
}

Script on the player character example:
if(collision.gameObject.name == "box")
{
     //do stuff here like destroy box and add point
}

The point is that if the script is on the box, you test for collision against the player character. If the script is on the player character, you test for collision against the box. It’s likely, from what I can tell, that you have the script attached to the same object that you’re testing collision for. If you have the script on the box and then test collision with the same object, it’s going to just null-out.

Hope this helps!