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 Juice-Tin · Oct 10, 2012 at 09:34 PM · variablestringnumber

String + Variable to retrieve a variable?

Here's a quick Example of what I would like to do.

 string name1 = "John";
 string name2 = "Mark";
 int personNum = 1;

 Debug.Log(this["name"+personNum]); // Prints John
 this["name"+2] = "Mike"; // Changes Mark to Mike
 Debug.Log(this["name"+2]); // Prints Mike

 //John
 //Mike

So as you can see, I'm wondering if it's possible to form variables using strings/variables/ints for temporary use (setting or getting variables). This works in other languages and obviously 'this["variable string"] is not the correct C# syntax.

Note: I'm only trying to access pre-defined variables using this method, not create them.

Thanks.

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
1
Best Answer

Answer by DaveA · Oct 10, 2012 at 09:42 PM

You could also create a Hash list and use the variable names as the keys.

Comment
Add comment · Show 3 · 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 Juice-Tin · Oct 10, 2012 at 11:42 PM -1
Share

That's true. But then the hash won't appear on the object script properties right?

Also I'm trying to store a bunch of sound effects in variables (through the public properties) and be able to easily access any with a string. Can't do that with arrays, and hash tables won't show up on property panel.

Thanks for the info though!

avatar image Eric5h5 · Oct 11, 2012 at 12:01 AM 0
Share

You can easily access sound effects in arrays. I would strongly recommend not using reflection and strings; it's quite slow and prone to error. If you want to use something "string-like" ins$$anonymous$$d of numbers, use an enum.

avatar image Juice-Tin · Oct 12, 2012 at 05:55 PM 0
Share

Yeah thanks, I was just hoping for a easy way to add sound effects to a script via the property manager, and then be able to access them with simple strings (without having to manually place them in hashtables, arrays, etc).

Oh well, thanks for the info. I'll use reflection for $$anonymous$$or things, and probably hash tables or arrays for larger, more time consu$$anonymous$$g processes.

avatar image
3

Answer by Juice-Tin · Oct 10, 2012 at 09:16 PM

Aha! Found it myself on some C# forums.

(string)this.GetType().GetField("name"+1).GetValue(this);

this returns the string variable called name1;

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 Bunny83 · Oct 10, 2012 at 11:34 PM 2
Share

I'm actually impressed that you found the reflection way ;) $$anonymous$$eep in $$anonymous$$d that reflection is slow and should be avoided if possible since it breaks most OOP rules. Usually there are better ways of doing what you want to do.

The simplest way is to use an array

 public string[] name;

Btw: the variable name "name" could be a problem since "name" is already a property of Component.

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

12 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

Related Questions

Edit Variables From Game! 1 Answer

Label dots in between numbers (currency formatting) 2 Answers

variables as GUI 1 Answer

Optimizing OnGUI 1 Answer

String as Variable name 0 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