- Home /
UnassignedReferenceException: i'm having troubles with a counter
I'm getting this error "The variable countertext of 'DestroyEgg' has not been assigned. You probably need to assign the countertext variable of the DestroyEgg script in the inspector.
#pragma strict
var counter : int;
var countertext : GUIText;
function Start()
{
counter = 0;
displayamount();
}
function displayamount()
{
countertext.text = "" + counter;
}
function OnTriggerEnter ( other : Collider)
{
if(other.tag == "Player")
{
Destroy(gameObject);
counter++;
displayamount();
print(counter.ToString());
}
}
here's the code
Answer by Graham-Dunnett · Mar 01, 2014 at 01:32 PM
You have a variable called countertext
but in your script code you do not assign it to a GUIText
. So, Unity has to assume that this variable is given a value in the Inspector. The run-time code hasn't found that assignment, so is recommending that you give it a value in the Inspector. If you have already done that, post a screen shot of the inspector.
Thanks for answering, but this is how it looks like in the inspector, i have dragged the Guitext that i'm using on the Countertext
@Woodchuck123456 - usually when this happens, you have your script attached to more than one game object (usually unintentionally). Click on the search box in the upper right corner of the Hierarchy view and type in the name of your script.
Well, that was why the error came up, now im running into problems, what i'm making is a pacman game, i want to be able to increment the counter and show it on the guitext every time the first person controller gobbles up an egg, but if i have the script on the first person controller, the eggs wont get destroyed, and if i have it on the eggs, i cant add GUItext... i have no idea how to proceed
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
4 errors in 1 script. Unexpected symbols and more, help please? 0 Answers
Error Building Player: 'aapt.exe' Win32Exception 3 Answers
BCE0044 unexpected char: 0xFEFF 2 Answers
£ Symbol won't work on Mac? 0 Answers