Question by
modernator24 · Apr 09, 2017 at 09:57 AM ·
scripting problemexceptionunassignedreferenceexcept
Variable has been assigned but has UnassignedReferenceExcecption
This is my script. You can see that declared some variables at top:
var QuestAccepted = false;
var TextBoxOnCheck: boolean = false;
var MessageBox: GameObject;
var TextBox: GameObject;
var QuestBox: GameObject;
var QuestText: GameObject;
function OnMouseDown() {
if(!TextBoxOnCheck) {
MessageBox.SetActive(true);
if(!QuestAccepted) {
TextBox.GetComponent.<Text>().text = 'Villager: Hello, some bandit have stollen my money. Could you retrieve it from them on the other side of the river?\n[F] to Accept, [X] to refuse.';
}
else {
TextBox.GetComponent.<Text>().text = 'Villager: Get going then!';
}
}
else {
MessageBox.SetActive(false);
}
TextBoxOnCheck = !TextBoxOnCheck;
}
function CloseMessageBox() {
MessageBox.SetActive(false);
TextBoxOnCheck = false;
}
function AcceptQuest() {
QuestText.GetComponent.<Text>().text = 'Active Quest: Recover the loot';
QuestAccepted = true;
TextBox.GetComponent.<Text>().text = 'Villager: Nice! I\'ll be waiting here, tell me when your job is done!';
//yield WaitForSeconds(3);
//CloseMessageBox();
}
function Update() {
if(Input.GetButtonDown('Use')) {
AcceptQuest();
}
else if(Input.GetButtonDown('Cancel')) {
CloseMessageBox();
}
}
And those are assigned in inspector as I dragged and dropped. When the game starts, all functions works well but this error message is in the console:
UnassignedReferenceException: The variable QuestText of Quest001 has not been assigned.
You probably need to assign the QuestText variable of the Quest001 script in the inspector.
UnityEngine.GameObject.GetComponent[Text] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GameObjectBindings.gen.cs:38)
Quest001.AcceptQuest () (at Assets/Scripts/Quest001.js:34)
Quest001.Update () (at Assets/Scripts/Quest001.js:44)
I removed the script and re-attached but still won't worked. If it's actually didn't assigned, it should be not work but it is working. What am I missing?
Comment
@hexagonius Can you post your comment as answer? I want to finish this question.
Best Answer
Answer by hexagonius · Apr 09, 2017 at 11:00 PM
Search the hierarchy for the script name, it's probably a duplicate you're not paid attention to.