I know this is asked a lot, but I need help with my script itself.
I am following a tutorial, where you move a person around, collect a coin, and if you get hit by an enemy u start over and need to reach the end goal to get to level 2. It had been working perfectly fine. Without touching my player script, I started to work on the UI, again following the tutorial. I hit play, go through my motions to collect my coin to test the UI, and suddenly I get errors saying that the object reference isn’t set. The items I am interacting with have the tags, everything is named correctly nothing had been touched between my trial run before starting the UI script and after I am done.
// Did we hit the enemy?
void OnTriggerEnter(Collider other)
{
if(other.CompareTag(“Enemy”))
{
GameManager.instance.GameOver();
If you need to see anything else, please let me know. I am a beginner at this and I just cannot see what changed. Visual Studio itself isn’t flagging that anything is missing.
It says "NullReferenceException: Object reference not set to an instance of an object
PlayerController.OnTriggerEnter (UnityEngine.Collider other)(at Assets/Scripts/PlayerController.cs:80)
and repeats for 73, and 89, which all are Enemy, Coin, Goal.
Use code tags.
Copy and paste the error as it gives line numbers
But, truth be told if I had to guess it is probably your GameManager.Instance might be null. Did you actually set a value for it?
I was so concerned about the script I didn’t look to see if the _GameManager was there, and once it again it got oddly deleted. However, I recreated it and I am still having this problem.
But this is my game manager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public int score;
NullReferenceException: Object reference not set to an instance of an object
GameManager.AddScore (System.Int32 scoreToGive) (at Assets/Scripts/GameManager.cs:29)
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:80)
NullReferenceException: Object reference not set to an instance of an object
GameManager.AddScore (System.Int32 scoreToGive) (at Assets/Scripts/GameManager.cs:29)
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:80)
Look rotation viewing vector is zero
UnityEngine.Transform:set_forward(Vector3)
PlayerController:Move() (at Assets/Scripts/PlayerController.cs:47)
PlayerController:Update() (at Assets/Scripts/PlayerController.cs:23)
NullReferenceException: Object reference not set to an instance of an object
GameManager.AddScore (System.Int32 scoreToGive) (at Assets/Scripts/GameManager.cs:29)
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:80)