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 huttch · Jun 06, 2012 at 02:20 PM · variablestringgetvarfrom

Get Variable From String

     var Prefabs : PrefabList = PrefabList();
     class PrefabList
     {
         var Pistol : GameObject;
     }
 
     function PickUpWeapon(WeaponName : String)
     {
        // need to get the variable Pistol inside Prefabs (Prefabs.Pistol)
        but get it from the string WeaponName, Example Prefabs.WeaponName
        so if WeaponName = Pistol then it would get Prefabs.Pistol, if
        WeaponName = Shotgun then it would get Prefabs.Shotgun, I am trying to
        do this so I don't have loads of if statements for every gun
        Prefabs.WeaponName.transform.position.y = 5;
     }
Comment
Add comment · Show 1
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 kolban · Jun 06, 2012 at 02:36 PM 0
Share

I don't believe what you are asking can be done. It seems that the flavor of JavaScript used by Unity uses data typing and tight binding. One way to achieve the effect you are after is to potentially use a hashtable or dictionary and use the named of the weapon as a key to look up the GameObject. See:

http://robotduck.wordpress.com/2009/11/04/88/

This way you could create a hashtable of dictionary of prefabs and access each unique one by key in one call.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by CHPedersen · Jun 06, 2012 at 02:34 PM

Using strings to define program behavior like that is kind of error prone and should be avoided, because strings are inherently free-form. This means you can write something like this elsewhere in your code:

 private void Start()
 {
     PickUpWeapon("Pastol");
 }

And the compiler won't catch the error, because "Pastol" is a valid string, even if it's misspelled, and should've contained the word "Pistol". That sort of stuff won't cause an error until runtime, and then you'll be left wondering why it didn't instantiate a pistol prefab like it was supposed to.

To do this right, you need to practice type safety and define an enum to hold your weapons. Then pass that in as the parameter, and switch over it in the method. Like this:

 public enum Weapons { Pistol, Shutgun, BaseballBat, Grenade} // As many as you want here. Define this outside the class definition.

Now you can define your method like this:

 public void PickUpWeapon(Weapons weaponToPickUp)
 {
     switch(weaponToPickUp)
     {
         case Weapons.Pistol:
             // Load Prefab.Pistol
             break;
         case Weapons.Shutgun:
             // Load Prefab.Shutgun
             break;
         case Weapons.BaseballBat:
             // Load Prefab.BaseballBat
             break;
         case Weapons.Grenade:
             // Load Prefab.Grenade
             break;
         default:
             break;
     }
 }

And call it elsewhere like this:

     PickUpWeapon(Weapons.Pistol);

This is type safety. It raises a compile time error if you write something like this:

     PickUpWeapon(Weapons.Grinade);

Because the compiler knows, before the program runs, that "Grinade" is not a part of the enumerated values in Weapons.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

variables as GUI 1 Answer

Help with GetType().GetField().GetValue() 1 Answer

Public variable hidden in the inspector 2 Answers

Is there a way to get the symbolic String name [editing name] of a variable at runtime? 1 Answer

How to get a value and keep it updated 2 Answers


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