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 YoungDeveloper · Jun 29, 2013 at 12:22 AM · arrayvariablereflectionnamedictionary

String as variable name, reflection or dictionary or other

What would be the fastest way, if we are talking about system performance here, of reading string as a variable name. So far I've read that there are options like reflection, dictionary or using an array. So I'm kinda confused right now, what are the differences between them and what method should i be using. And just so for the idea, what i would like to achieve:

 //Class something
 public string a="box";
 public int box = 10;

So usual way of accessing variable box would be

 something.box;

But instead, how would i go if I wanted to achieve this type of method of accessing the variable box(syntax is wrong here of course, It's just for the idea)

something.a; //And it would read it as something.box;

Comment
Add comment · Show 2
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 AVividLight · Jun 29, 2013 at 03:02 AM 0
Share

Hey YoungDeveloper,

I'm sorry, but I don't really understand your question. Would it be possible for you to either explain it a little more, or give us a reason why you'd want a variable with a variable name.

Gibson

avatar image YoungDeveloper · Jul 01, 2013 at 06:02 PM 0
Share

Hey, Yea, I sure asked that in a quite not understandable way, sorry. So the main thing i wanted to understand here is, is there a way of executing a code from string. The reason i want to do that, is because i have a class of many, many items (each in its own class), amount of items will sure be over hundreds over time. So that would sure be a performance pain, if i'd check all classes, using if's, to get data about what kind of object is that (what have i picked up), because information about all items is stored in one script. So i was thinking creating (script) a universal way of getting data of what pick-able item have you picked up. Because if so, adding new items (like lets say potions) to game will be much more faster in all ways, because all what i will have to do create a new little class inside item class.

So if I had a

 string a = "item.weeder_1.name"; 

How to execute it as piece of code ? If this is still not enough, i will post an example script, hehe.

Thanks, $$anonymous$$arl.

2 Replies

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

Answer by iwaldrop · Jul 01, 2013 at 06:22 PM

You could defiantly use a dictionary. Why you'd want to, however, is a question you should really consider.

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DictionaryMethods : MonoBehaviour
 {
     private Dictionary <string, Action> methods;
     
     void Awake()
     {
         methods = new Dictionary <string, Action>();
         methods.Add("MethodOne", MethodOne);
         methods.Add("MethodTwo", MethodTwo);
     }
     
     void Start()
     {
         methods["MethodOne"]();
     }
     
     void MethodOne()
     {
         Debug.Log("MethodOne()");
     }
     
     void MethodTwo()
     {
         
     }
 }
 
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 YoungDeveloper · Jul 01, 2013 at 09:58 PM 0
Share

Thanks @iwaldrop and @BlueRaja for your answers. You are right, i will find another way around this problem.

Thanks again.

avatar image
2

Answer by BlueRaja_2014 · Jul 01, 2013 at 06:41 PM

You can do this using reflection, but it will be significantly slower than resolving the property at compile-time. I agree with @iwaldrop that there is almost certainly a better way to do whatever it is you're trying to do.

But to answer your question, assuming instance in the name of your class instance and myProperty is the name of public property whose value you want, just do

 instance.GetType().GetProperty("myProperty").GetValue(instance, null);
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 Lisa.s · May 26, 2016 at 10:59 AM 0
Share

I have a string were I stored the name of a script and i want to define an instance of that script to access some of its attributes , how can I do that using reflection? I mean how can use the string which hold the script name to define an instance of it ? thanks in advance

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

18 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

Related Questions

Storing a variable and it's name 3 Answers

Is it possible to change the element name in an Array List? 1 Answer

How to make an array of Interactive Clothes in one variable 0 Answers

Editting a custom javascript class in the inspector 1 Answer

Trying to convert a JC script to C # 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