So here is the thing. My goal is to make the player destroy when it hit the obstacle. Here’s the code for that
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyPlayer : MonoBehaviour
{
void OnCollisionEnter(Collision other)
{
if(other.gameObject.tag == "Obstacle")
{
Destroy(gameObject);
}
}
}
But then when I try to play it and try to hit the player into an obstacle, it throws a lot of error saying “Missing Reference Exception: The object type ‘Transform’ has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.”
Can someone help me? I’m new to Unity and C#
You should copy and paste the error here so we can see all the info on it. Chances are you are still doing some interaction with the player in another script that is throwing the error because you are destroying the player.
You can also double click most errors to have it take you to the line of code that is throwing the error.
If you plan to destroy the player, you need to consider what else you need to do. Do you have other scripts targeting the player? Do you have GUI you need to display? Pause the game? Stop player movement? What other things do you need to happen when a player dies.
I would also suggest, unless this is a single life game, that you consider not destroying the player, but resetting them instead. You’ll maintain your references to the player, you just need to do some “clean up” work like resetting player position, health, resetting enemies or collected items, whatever it is that works for your game.
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update()
{
transform.position = player.position + offset;
}
}
I add this code as a component to my main camera, score UI, and trail (Some kind of smoke which is made from particle system)
This is the script that throwing an error. I don’t have any GUI to display, I don’t want to pause the game, And yes I want to stop player movement. Yes even tho this is a singlie life game, I still want to destroy the player.
Then you have to stop the camera from calling player.position. Exactly as the error tells you. You can either disable the FollowPlayer script when the player dies or add a bool that you set to false so even when Update runs, it no longer tries to follow your player.
If you are purposely making player null by destroying it, then just don’t try using it when it is null.
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 offset;
// Update is called once per frame
void Update()
{
if (player != null)
{
transform.position = player.position + offset;
}
}
}
Otherwise if you want to keep using it, don’t make it null. Either assign something else to player before destroying it, or don’t destroy it.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:
drag it in using the inspector
code inside this script initializes it
some OTHER external code initializes it
? something else?
This is the kind of mindset and thinking process you need to bring to this problem: