Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gregdevice · Apr 04, 2013 at 07:06 PM · variablefor-loop

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);
 
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Apr 04, 2013 at 08:53 PM 0
Share

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).

avatar image gregdevice · Apr 05, 2013 at 01:55 AM 0
Share

Yes, true. That's the way I normally have it. I've been trying different methods to solve this issue. Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
      
     }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gregdevice · Apr 05, 2013 at 03:34 AM 0
Share

Elite$$anonymous$$ossy, the break was what I needed. Excellent. Glad you came along. Thanks!

avatar image
-1

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

  1. put your var getStats : itemStats outside the for loop.

  2. var getStats : itemStats = getObj.gameObject.GetComponent(itemStats); << Error itemStats not defined

  3. 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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Apr 04, 2013 at 08:48 PM 0
Share

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.

avatar image gregdevice · Apr 05, 2013 at 01:58 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges