I just got Unity about a week ago, and I am quite new to C# as well. I am confused about the fact that unity is giving me an error saying: NullReferenceException: Object reference not set to an instance of an object.
The line it gives me is line 108 of my PlayerController script where there is a function call to resume the game from another script (the script that controls the pause UI), but this function definitely exists. Can anybody help me with this?
Also, most of my variables are a mess, so it may be a little hard to read.
My Player Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OnButtonPressScript : MonoBehaviour
{
public PlayerController pc;
public void QuitGame()
{
Application.Quit();
Debug.Log("QUIT GAME");
}
public void Resume()
{
pc.playing = true;
Time.timeScale = pc.timescale;
Cursor.lockState = CursorLockMode.Locked;
gameObject.SetActive(false);
}
public void Activate()
{
gameObject.SetActive(true);
}
}
My UI contol script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OnButtonPressScript : MonoBehaviour
{
public PlayerController pc;
public void QuitGame()
{
Application.Quit();
Debug.Log("QUIT GAME");
}
public void Resume()
{
pc.playing = true;
Time.timeScale = pc.timescale;
Cursor.lockState = CursorLockMode.Locked;
gameObject.SetActive(false);
}
}
dang that’s quite elaborate for a week, i’ve been doing it for longer but still can’t do what you do haha.
You’ve defined buttonpress as OnButtonPressScript, but have you actually dragged and dropped that script into your public variable?
e.g. if your script is placed on the player, click on the player, scroll down to where the playercontroller script is, and check the variable buttonpress.
I’m somewhat sure that when you look at your script, it’ll say (none) where the buttonpress variable is.
If it’s there, like button press: (OnButtonPress) or something like that, go into play mode. Check again.
If the script is not on your player(it shouldn’t be, it should be on another gameobject like gamemanager instead of on the player), and your player dies and respawns(e.g. gets deleted before a prefab of it is spawned; because the two scripts are not on the same gameobject, the reference will be lost.
So play your game, and before you call the function, check your variable buttonpress under the playercontroller script on the gameobject you placed it under to make sure it doesn’t say (none)
Make sure you also check that it’s the correct script OnButtonPress.
NullReferenceException basically means that you’re trying to reference something you never put the reference to.
Please always include the exact message which is shown in the console. It’s usually better than paraphrasing what’s contained in the error log, i.e. most of the time it contains the entire stack trace which includes script names, member names, lines, etc…
Please also make sure the lines in your editor (and thus the ones mentioned in the error messages) are the same as the one in your code snippets that you add (not a big problem in this particular case as it only appears to be off by 1, but sometimes it’s not as obvious at first).
Note that the error message does not mean that a function doesn’t exist (that would be an error at compile time, not a runtime error). If line 108 is actually line 107 in your post, then the only thing that could cause a NullReferenceException is an unassigned buttonPress. Make sure it’s assigned on all instances that you need, or delete instances that you have added by mistake.
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.
This is the kind of mindset and thinking process you need to bring to this problem: