using System.Collections;
using UnityEngine;
public class Respawn : MonoBehaviour {
public int respawnTime = 5;
void OnCollisonEnter(){
this.GetComponent<SphereCollider> ().enabled = false;
this.GetComponent<MeshRenderer>().enabled = false;
Invoke("respawn", respawnTime);
}
void respawn(){
this.GetComponent<SphereCollider> ().enabled = true;
this.GetComponent<MeshRenderer> ().enabled = true;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I think I see the problem now. Can’t believe I didn’t see it at first! You’ve misspelled OnCollisionEnter
as OnCollisonEnter
, leaving off the second ‘i’ after ‘s’ in ‘Collision’. Also I think you’ll need to add the 'Collision` parameter to the method signature, like this:
void OnCollisionEnter(Collision col) {
this.GetComponent<SphereCollider>().enabled = false;
this.GetComponent<MeshRenderer>().enabled = false;
Invoke("respawn", respawnTime);
}
If the method signature doesn’t exactly match what is specified in the docs, then Unity doesn’t know that the method exists and so when a collision occurs, it will not know to call it. Note that you need to do this even if you do not use the parameter within your function.
Assuming that it’s valid to use the OnCollisionEnter method without parameters (I never tried it). In any case you can copy paste the example code from the docs to make sure, it should work: OnCollisionEnter
The most common issue with collision and trigger callbacks is to make sure:
- One of the two colliders, requires an active rigidbody, that is part of the physics simulation.
- When using OnCollisionEnter, the isTrigger property on the collider should not be set, if using OnTriggerEnter, it should be set.
Is there a Rigidbody on at least one of the objects you are colliding together?