I'm creating the platform falling effect by unchecking the isKinematic but I keep getting the error : "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Here is my code:
// Update is called once per frame
void Update()
{
}
private void OnCollisionExit(Collision collision){
if(collision.gameObject.tag == "Player")
{
Fall();
Invoke("Fall", 0.2f); //delay 0.2 s chu y dau va viet thuong
}
}
void Fall(){
GetComponent<Rigidbody>().isKinematic = false;
Destroy(gameObject,1f);
}
Can anyone know how to fix this problem? Thank you.
CodePudding user response:
The flow of your current code is as following:
- Called Fall() once (After 1 second from that call the gameObject will be destroyed).
- You are using Invoke to call Fall() after 0.2 seconds while you have already a task to destroy the gameObject from the first call.
Update: I assumed that you want to set kinematic false immediately. I updated the piece of code :)
Code
using UnityEngine;
public class Q69205071 : MonoBehaviour
{
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
//Fall(1f);
/// since you want everything to be executed after 0.2 seconds
/// you could use Invoke or coroutines.
Invoke("Fall", 0.2f);
}
}
private void Fall()
{
GetComponent<Rigidbody>().isKinematic = false;
Destroy(gameObject, 1f); // 1 seconds delay before destroying the gameObject
}
}
Happy Coding :)