I’m looking for a bit of help.
This is in 3D
I have it to where a cube (Note the tag is EnemyCube ) will seek out my player and try to collide with them.
I want it to where the character (Note the tag for the player is Player - Surprise, surprise ) will die after the Cube collides with them.
This is what I have tried for the script and nothing seems to work.
using UnityEngine;
using System.Collections;
public class WillDieOnContact : MonoBehaviour
{
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "Player")
{
Destroy(col.gameObject);
}
}
}
AAAAAANNNNDDDDDD
using UnityEngine;
using System.Collections;
public class WillDieOnContact : MonoBehaviour
{
void OnCollisionEnter (Collision col)
{
if(col.gameObject.name == "EnemyCube")
{
Destroy(col.gameObject);
}
}
}
Any help…?
P.S. this is in C#
“OnCollisionEnter not working” is one of the most commonly asked questions on this site, and the checklist to determine the problem is almost always the same:
Do both objects have colliders on them?
Are those colliders not marked as Is Trigger?
Does at least one of the objects also have a rigidbody component attached?
Is that rigidbody not marked as Is Kinematic?
Fappp
April 25, 2015, 8:56am
4
Way to go, and this is adviced for all triggering events:
Add a box collider to you fps player, mark as trigger.
Make sure you delete the right object, in your script you delete the col == EnemyCube.
Change your code accordingly:
void OnTriggerEnter (Collider col)
{
if(col.gameObject.name == “EnemyCube”)
{
Destroy(this.gameObject);
}
}
pls feedback if works!
perhaps instead of gameObject.name you should use gameObject.tag
using UnityEngine;
using System.Collections;
public class DestroyObject : MonoBehaviour {
void OnCollisionEnter (Collision collision){
if (collision.gameObject.tag== “Player”)
Destroy(this.gameObject);
}
}