- Home /
Collisions won't register?
Hi. I'm making a simple Pacman-like game where I want an object to be replaced with another when the player collides with it. I've figured out that I should destroy the original object and spawn the other object in the same transform. However, for whatever reason, I can't get the game to register the collision at all. I'm quite stumped, as it seems to be an incredibly simple piece of code. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HouseScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnCollisionEnter(Collider col) {
if (col.gameObject.CompareTag ("Player")) {
Debug.Log ("awake");
Object.Destroy (this.gameObject);
}
}
// Update is called once per frame
void FixedUpdate () {
}
}
Can anyone please help out?
Answer by ironZinc · Sep 03, 2017 at 04:35 AM
Like jasonlu00 said, you might want to change the Collider to Collision, but also you might to make sure that BOTH the player and the object has a rigidbody and a collider. If it still doesn't work, go to the rigidbody component on the player and set the Collision detection to continuous dynamic and set the other object's collision detection to continuous (this will make sure they collide even if the player is moving fast).
HOWEVER, what you want to do might be better done with OnTriggerEnter: All you need to do is tick the isTrigger option on the object's collider component and change the OnCollisionEnter to OnTriggerEnter.
void OnTriggerEnter(Collider col)
{
if (col.gameObject.CompareTag ("Player"))
{
Debug.Log ("awake");
Object.Destroy (this.gameObject);
}
}
Answer by jasonlu00 · Sep 03, 2017 at 01:03 AM
There are couple things you need to check:
Make sure you have a RigidBody component on your Player
Make sure you have a Collider(ex: BoxCollider) on your HouseScript gameObject
In your example code, you should be using Collision Type as your callback type.
void OnCollisionEnter(Collision collision){
if(collision.gameObject.CompareTag ("Player")) {
Debug.Log ("awake");
Object.Destroy (this.gameObject);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612134058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
bugs in collisions 0 Answers
Flip over an object (smooth transition) 3 Answers