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 RoofTurkey · Apr 10, 2015 at 10:32 AM · stringvariable name

Build variable name using string

For my game I want to load a variable based on a string. The string is made up of standard text plus the ID of the specific object (so I only need one script that can handle actions for lots of objects). The script looks like this (and doesn't function):

 string reference = "list" + id;
 List<int> thisLevels = OtherStaticScript.reference;

It's pretty obvious to me that it won't work like this, but still it was worth a try.

So is there a way to make this work or a different approach to this issue? Thank you in advance for your help!

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 AlwaysSunny · Apr 09, 2015 at 09:39 PM 2
Share

Not sure what you're trying to accomplish, but when I hear "get/load/find variable by name using a string" the best answer is usually: you shouldn't want to do that.

This can be accomplished in some cases using Reflection. However, if you find yourself in need of this feature, it's often a sign you're not leveraging the features of the language or following OOP / Unity principles.

Based on your (not nearly thorough enough...) description, I assume you're creating some kind of library of objects at design time, and you'd like to be able to ask for them by name at runtime. This is usually handled by somehow getting those objects into a collection you can search at runtime by looping over the elements & checking against some property like a string name. $$anonymous$$ajor projects use databases for much of this, but it's not necessarily a must, especially for a smaller production.

The best way to put your objects into that searchable collection varies by case, and depends on what kind of objects they are as well as how you're designing / handling / using them. Gonna have to do some research based on your needs. Welcome to the party. ;)

avatar image RoofTurkey · Apr 10, 2015 at 07:34 AM 0
Share

Thank you for your answer. I will try to make it more inline with the 'usual' way then :) .

avatar image RoofTurkey · Apr 10, 2015 at 04:39 PM 0
Share

Thanks everyone for the quick answers, I think I'm gonna stick with doing it the 'right' way then as mentioned before.

2 Replies

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

Answer by Raresh · Apr 10, 2015 at 11:57 AM

I had a similar issue, what you need is a Dictionary. It is basically a list that has a 'key'. The key can be an int, string, char you name it. With the key you can search for fields in the Dictionary.

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

Answer by Kriogenic · Apr 10, 2015 at 12:12 PM

I believe you can do something like

 string reference = "list" + id;
 list<int> thisLevels = OtherStaticScript.GetType().GetProperty(reference).GetValue(OtherStaticScript,null);

I actually don't think this works on arrays or lists but it should be adapted I'm on my phone so hard to write it out complete... GL

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 Kriogenic · Apr 10, 2015 at 12:15 PM 1
Share

I should point out that AlwaysSunny way is correct and you should avoid doing this but its there if you need it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Change object texture using String [Java] 1 Answer

Separating two strings 1 Answer

convert string to list of lists 1 Answer

Is it possible to name a list by a string variable? 2 Answers

Creating a Token At function 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