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 () {
}
}
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.