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?
What this guy said. An alternative is that he declared 'weapon' elsewhere, and is only setting it in this method.
– JChilton@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.
– robertbuI 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.
– GKdev1980UnityScript 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.
– rutter