Problem in Collider

Hello there,
In my game, i have to collect my keys. So i have a Key Prefab with Box collider, and have Character Prefab with Character Controller, the Player tag is “Character”, Key name is “Key”. I have used OnColliderEnter function to destroy that key. But it doesn’t work. Here is the Snippet Code.

void OnCollisionEnter(Collision Others)
	{
		if(Others.gameObject.name == "Key")
		{
			Debug.Log ("Key Collected");
			Destroy (Others.gameObject);
			Key_Text_Effect_Temp = Instantiate (Key_Text_Effect, new Vector3(transform.position.x, transform.position.y + 0.2f, transform.position.z + 0.5f), transform.rotation) as GameObject;
			Key_Texture_Time += Time.deltaTime;
			if(Key_Texture_Time >= 2)
			{
				Destroy (Key_Text_Effect_Temp);
				Key_Texture_Time = 0;
			}
		}
	}

I have aa other Object with Box Collider, its working fine with Character Object.

-Prasanna

Have you checked that the name of the other gameobject is exactly “Key”? Because if you are instantiating them they may have names like “Key(clone)”. Also, setting the key collider as isTrigger will help.

I suggest you use a layer for items (or even just for keys). That way, whenever you hit an object within that layer you don’t have to worry about the names.

@Andres Fernandez, thanks for your advise. I found the Solution, when the Character have Character Controller Component, the Syntax should be like this.

void OnControllerColliderHit(ControllerColliderHit Others)
	{
		if(Others.gameObject.tag == "Key_New")
		{
			Debug.Log ("Key Collected");
			Destroy (Others.gameObject);
			Key_Text_Effect_Temp = Instantiate (Key_Text_Effect, new Vector3(transform.position.x + 3.0f, transform.position.y + 0.2f, transform.position.z + 0.5f), transform.rotation) as GameObject;
			Key_Texture_Time += Time.deltaTime;
			if(Key_Texture_Time >= 2)
			{
				Destroy (Key_Text_Effect_Temp);
				Key_Texture_Time = 0;
			}
		}
	}

So this will Help your Character to Collide with other Collider like Box Collider, Capsule Collider, etc…,

-Prasanna