- Home /
UnassignedReferenceException
Hi! So I'm trying to make a quest for the player and wrote the following code:
// Applied to Quest NPC
public class Quest001 : MonoBehaviour
{
public string questName = "Coin Collect";
public string questInfo = "Collect all the coins";
public float range = 3f;
public Transform player;
// I need this to set the questText to active
public GameObject questPanel;
// The text object to holder the questInfo
public Text questText;
// Same purpose like the two above
public GameObject activeQuestPanel;
public Text activeQuestText;
void Start()
{
questText.text = questInfo;
activeQuestText.text = "Active quest: " + questName;
}
void Update()
{
RaycastHit hit;
if (Input.GetMouseButtonDown(2) && Physics.Raycast(player.position, player.forward, out hit))
{
if (hit.distance <= range)
{
questPanel.SetActive(true);
activeQuestPanel.SetActive(true);
}
}
}
}
I assigned all of the value in like so:
But when I play my game, it's saying "UnassignedReferenceException: The variable player of Quest001 has not been assigned" but I can play my game fine, it works. But this exception kept showing on the console window.
Is it a bug or I did something wrong?
Note: The two text variable in the image is not the same Text.
Thanks alot! I appreciate it!
the player either gets destroyed somewhere (and replaced with a new one so you did not notice).
this code is not doing that
@hexagonius Wehn I play my game, it does give me an error but I can play it O$$anonymous$$ like it was some sort of bug or something. I'm very sorry to not mention that.
ok, try to be more clear. it does not make sense at all. the player is accessed in update so you should be spammed. apparently you're seeing the error, what, once?
which line?
my first question would be, how dots the variable recover from null?
and check out using logs and breakpoints as those are key to finding causes yourself.
Answer by Sakeus · Dec 23, 2018 at 02:57 PM
It seems to me that "Player" may have been unassigned during runtime by another script, try making player private and serialize it. There's is nothing in your provided script that should cause this error.
[SerializeField]
private Transform player;
Your answer
