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 asdf123 · Feb 09, 2014 at 12:15 PM · arrayweaponsetactiveforeach

Array simple problem

What's wrong with the second code? the two codes do the same, but in the second code, the script attached to the GameObject doesn't play or dont work, in the first script all works correctly but when i have much weapons the script will be giant :/

// First Code

 if(currentSecondaryWeapon == "M9") {
             secondaryWeapons[0].SetActive (true);
             secondaryWeapons[1].SetActive(false);
         }
 else if(currentSecondaryWeapon == "Glock") {
             secondaryWeapons[0].SetActive (false);
             secondaryWeapons[1].SetActive(true);
         }

// Second Code

 if(currentSecondaryWeapon == "M9") {
             foreach(GameObject secondaryWeapon in secondaryWeapons) {
                 secondaryWeapon.SetActive (false);
             }
             secondaryWeapons[0].SetActive (true);
         }
 else if(currentSecondaryWeapon == "Glock") {
             foreach(GameObject secondaryWeapon in secondaryWeapons) {
                 secondaryWeapon.SetActive (false);
             }
             secondaryWeapons[1].SetActive (true);
         }
Comment
Add comment · Show 3
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 Bagul3 · Feb 09, 2014 at 12:19 PM 0
Share

Don't know if this is correct, but are you not setting all gameobjects to false when you run your foreach?

 foreach(GameObject secondaryWeapon in secondaryWeapons) {
           secondaryWeapon.SetActive (false);
          }
avatar image asdf123 · Feb 09, 2014 at 12:22 PM 0
Share

i want to set all false and then i set the selected weapon true

avatar image DajBuzi · Feb 09, 2014 at 01:06 PM 0
Share

I think it would be better to just instantiate/destroy instance weapon then just activate/deactivate.

Or try something like this:

foreach(GameObject SecWeapon in secWeapons){ if(curSecWeapon.name != SecWeapon.name) SecWeapon.SetActive(false); else SecWeapon.SetActive (true); }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Fabkins · Feb 09, 2014 at 01:04 PM

I cannot see anything obviously wrong with the code but I would suggest is you trace bomb the code by adding Debug.Log line at every point you set something.

I would also suggest you structure your code such that you first run the loop to clear the setactive flag (currently you do this in a for loop).

Then you use a "switch" statement to set active.

eg

 foreach(GameObject secondaryWeapon in secondaryWeapons) 
 {          
    secondaryWeapon.SetActive (false);         
 }

 switch(currentSecondaryWeapon )
 {
 case "M9":
   secondaryWeapons[0].SetActive (true);
   break;
 case "Glock":
    secondaryWeapons[1].SetActive (true);
    break;
 }

Lastly, instead of brute force clearing every weapon, why dont remember which is the last weapon you selected and clear it.

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 asdf123 · Feb 09, 2014 at 02:24 PM 0
Share

with enum the game goes the same way, i can change weapon, but the script attached to it don't play, the OnGUI of the script play, but the inputs and etc dont work

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Plane - 10x10 grid - Questions about it. 1 Answer

Array.Count returning 0 out of the foreach and 4 inside 1 Answer

Array foreach problem 1 Answer

Disable All Transform In List 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