- Home /
Another "BCE0005: Unknown identifier:" error (following tutorial)
I know it's been discussed again and again, I'm just curious.
I've started the "Digital Tutors - Introduction to Scripting in Unity" tutorial series, still going through some basic stuff, at some point there's a script when he first discusses custom functions:
function Update () { if(Input.GetKeyUp("space")) { WeaponSearch(); } }
function WeaponSearch() { weapon = Random.Range(1, 5);
switch(weapon)
{
case 1:
weaponName = "sword";
DisplayMessage(weaponName);
break;
case 2:
weaponName = "axe";
DisplayMessage(weaponName);
break;
case 3:
weaponName = "dagger";
DisplayMessage(weaponName);
break;
case 4:
weaponName = "bow";
DisplayMessage(weaponName);
break;
default:
Debug.Log("You need to find a weapon!");
}
}
function DisplayMessage(weaponName) { Debug.Log("You found " + weaponName + "!"); }
when I copy it and try to add it to a gameObject, I get an error BCE0005: Unknown identifier: 'weapon'. Now I've done some amateur coding in the past, I'm not completely new to some basic notions, I personally find it logical to get an error, since the variable is not declared earlier. I edited the line to: var weapon = Random.Range(1, 5); and it works just fine.
The thing is, that watching the video tutorial, he uses the unedited script and it works just fine (!!!!!!). That is WITHOUT the "var" in front of the "weapon = Random.Range(1, 5);". He get no errors or anything.
I'm wondering why is that. Does it have anything to do with the Unity version or anything? I go on, and he introduces us to loops with a statement: for (i=1; i<5; i++); without declaring the 'i' variable up ahead. And it works just fine!!
What exactly is going on?
Answer by robertbu · Dec 09, 2013 at 07:57 PM
As an educated guess, I'll bet you are using #pragma strict at the top of the file and the tutorial is not. There are sound performance and quality reasons for using #pragma strict and declaring your variables.
There is some reading here about static vs. dynamic typing and the implication for performance:
http://docs.unity3d.ru/ScriptReference/index.Performance_Optimization.html
What this guy said. An alternative is that he declared 'weapon' elsewhere, and is only setting it in this method.
@JChilton - possible, but the for() loop also works. It would be unlikely for 'i' to be declared outside this function. That's what clued me in that this is likely a dynamic typing situation.
I went through a brief search on
#pragma strict
, it is noted that you have to explicitly declare the variable type. I've created a test script, I would say that's half true. It allows me to declare and instantiate a variable like this:
var x = 3; var y = 5.5; var z;
without me needing to declare that x is an integer or y is a float.
So I guess I can get away with type declaration as long as I declare the variable name and/or an initial value?
But is this really a good practice? I mean, maybe I could save calculations and resources in unity if I declared the type myself.
UnityScript will try to infer a type, in the exact scenario you described: when you declare the variable and give it a value on the same line. It's not always right, but it's usually a good guess.
The type inference happens at compile-time, so it's all set up and ready before the game ever runs.
On the other hand, if strict mode is off, dynamic typing is enabled, and you're back to runtime type checks. Saves the developer some trouble, at the cost of performance penalties.
Your answer
Follow this Question
Related Questions
Only in Mobile BCE0005: Unknown identifier 2 Answers
BCE0005 unknow identifier 'rigidbody' 0 Answers
BCE0005 Unknown identifier issue 1 Answer
Publish 2D tutorial to IOS problem 0 Answers
Error building 2D Gameplay tutorial 0 Answers