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
1
Question by darkcookie · Jul 31, 2013 at 09:55 AM · gameobjectlistarrays

Switch Case List of Game objects ??

I know its not possible to use a list or array to switch case ,so Im wondering how can I possibly switch case a list of game objects ? there's a function that assigns the the gameObject "primaryWeapon" from the list of game objects ...so basically its obtains its reference from the Weapons list ....

 public List<GameObject> Weapons = new List<GameObject>();
 
 public List<GameObject> WeaponDropPrefab = new List<GameObject>();

 public GameObject PrimaryWeapon;


            switch (PrimaryWeapon){
                  
                 case "PotatoLauncher":
                     WeaponDrop = WeaponDropPrefab.Find(w => string.Equals(w.name,"PotatoLauncher"));
                     Instantiate(WeaponDrop,transform.position,transform.rotation);
                     PrimaryWeapon=null;
                     break;
                     
                  case "SlingShot":
                     WeaponDrop = WeaponDropPrefab.Find(w => string.Equals(w.name,"SlingShot"));
                     Instantiate(WeaponDrop,transform.position,transform.rotation);
                     PrimaryWeapon=null;
                     break;
                     
                     case "Sphere1":
                     WeaponDrop = WeaponDropPrefab.Find(w => string.Equals(w.name,"Sphere1"));
                     Instantiate(WeaponDrop,transform.position,transform.rotation);
                     PrimaryWeapon=null;
                     break;
                 }
Comment
Add comment
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

2 Replies

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

Answer by nixcs2512 · Jul 31, 2013 at 11:15 AM

Basically you only need set switch (PrimaryWeapon.name) because all your cases are set with string type.Or if you want to keep switch (PrimaryWeapon) you need to change your code a bit:

 GameObject PotatoLauncher = WeaponDropPrefab.Find(w => string.Equals(w.name,"PotatoLauncher"));
 GameObject SlingShot = WeaponDropPrefab.Find(w => string.Equals(w.name,"SlingShot"));
 GameObject Sphere1 = WeaponDropPrefab.Find(w => string.Equals(w.name,"Sphere1"));
 switch (PrimaryWeapon){
         case PotatoLauncher:
           Instantiate(PotatoLauncher,transform.position,transform.rotation);
           PrimaryWeapon=null;
           break;
 
        case SlingShot:
           Instantiate(SlingShot,transform.position,transform.rotation);
           PrimaryWeapon=null;
           break;
 
        case Sphere1:
           Instantiate(Sphere1,transform.position,transform.rotation);
           PrimaryWeapon=null;
           break;}
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 darkcookie · Jul 31, 2013 at 11:28 AM 0
Share

ohh ok thanx:)

avatar image
0

Answer by RandomiaGaming · May 17, 2019 at 06:59 PM

visual studio is complaining because it says a constant value is expected in the place of Any GameObject Variable.

switch(Any GameObject){

case Any GameObject Variable: Method(); break;

} @nixcs2512

Comment
Add comment · 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

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

17 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

Related Questions

How to create an array of Transforms 1 Answer

How To Get A Reference To All Nearby GameObjects? 1 Answer

A node in a childnode? 1 Answer

How to add and access objects in array? 1 Answer

GetComponent for gameobject list alternative 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