- Home /
UnityScript Variable Scope?
Hi all, I've striped down this code to see if anyone can shed some light on what I am not seeing here. The bottom print statement does not output as expected (or at least what I expect). It should output the results from the if statements but instead it outputs another gameobject in the scene, even if invSlot == 1 the output does not match. It works fine inside the if statement just not outside. (note: also does the same thing if I first declare the "var getStats : itemStats" outside of the for loop)
function EquipWeapon(invSlot : int){
var getObj : GameObject;
for (getObj in WeaponPool){
var getStats : itemStats = getObj.gameObject.GetComponent(itemStats);
if (invSlot == 1){
if (getStats.itemType == inventory.weapon1Stats.itemType){
print ("getStats.itemName= " + getStats.itemName);
}
}
if (invSlot == 2) {
if (getStats.itemType == inventory.weapon2Stats.itemType){
print ("getStats.itemName= " + getStats.itemName);
}
}
}
print ("getStats.itemName= " + getStats.itemName);
}
This isn't related, but this:
var getObj : GameObject;
for (getObj in WeaponPool){
should be:
for (var getObj in WeaponPool){
Unlike other loops in Unityscript, for/in loops have local scope for the iterator, and the way you have it, there are two getObj
variables, one inside the loop and one (which is unused) outside the loop. Unfortunately the Unityscript compiler doesn't tell you about unused local variables, so you won't get a warning about this (which is especially annoying since at one point the compiler did give warnings about unused local variables; no clue why anyone thought it might be a good idea to remove that).
Yes, true. That's the way I normally have it. I've been trying different methods to solve this issue. Thanks!
Answer by EliteMossy · Apr 05, 2013 at 02:33 AM
This is a simple fix
function EquipWeapon(invSlot : int){
var getStats : itemStats
for (var getObj in WeaponPool){
getStats = getObj.gameObject.GetComponent(itemStats);
if (invSlot == 1){
if (getStats.itemType == inventory.weapon1Stats.itemType){
print ("getStats.itemName= " + getStats.itemName);
}
}
if (invSlot == 2) {
if (getStats.itemType == inventory.weapon2Stats.itemType){
print ("getStats.itemName= " + getStats.itemName);
}
}
}
print ("getStats.itemName= " + getStats.itemName);
}
Also the last print statement will print the last object ever selected in the for loop. Which is the last object in the array/list. You could break out of the for loop after setting the item correctly
function EquipWeapon(invSlot : int){
var getStats : itemStats
for (var getObj in WeaponPool){
getStats = getObj.gameObject.GetComponent(itemStats);
if (invSlot == 1){
if (getStats.itemType == inventory.weapon1Stats.itemType){
print ("getStats.itemName= " + getStats.itemName);
break;
}
}
if (invSlot == 2) {
if (getStats.itemType == inventory.weapon2Stats.itemType){
print ("getStats.itemName= " + getStats.itemName);
break;
}
}
}
print ("getStats.itemName= " + getStats.itemName);
}
Elite$$anonymous$$ossy, the break was what I needed. Excellent. Glad you came along. Thanks!
Answer by Nachtmahr · Apr 04, 2013 at 07:31 PM
The Curly Braces define a Scope { if you declare something here } it gets lost here
put your var getStats : itemStats outside the for loop.
var getStats : itemStats = getObj.gameObject.GetComponent(itemStats); << Error itemStats not defined
for (getObj in WeaponPool) >> for(var Type : varName in somelist/array) >> Error getObj is also undefined in this case
I wonder why your script execute at all ^^
Edited: had wrong Syntax in the for loop
1) Unityscript does not work like that.
2) "itemStats" is presumably the name of a script and does not need to be defined anywhere. It's not an error, though ideally for the sake of proper style it should be called "ItemStats" and not "itemStats".
3) You're confusing C# with Unityscript as far as the type/varName order goes. Also you can leave out var with for/in loops (even with #pragma strict), and specifying the type is unnecessary since it's inferred from the type of the collection.
Thanks for the comments. Either way I define "getStats", whether inside the loop or outside, the results for are the same: "getStats.itemName" is correct inside the loop but is not correct outside the loop. Still trying to figure it out.
Your answer
Follow this Question
Related Questions
(C#) For loop variable value not changing 0 Answers
Variable goes very high in update method 1 Answer
naming a variable with a number 1 Answer
x=x Assignment made to same variable 3 Answers
Sharing booleans between 2 scripts 1 Answer