Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

How to get all variable names of Component?

Discussion in 'Scripting' started by shinriyo_twitter, Oct 27, 2011.

  1. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    Hello

    I'd like to get all values of Component via script.

    http://unity3d.com/support/documentation/ScriptReference/Component.html
    but, Component class didn't have getting variable names method.


    I wanna like that
    If Hoge class have "a", "b" and "c" variables, it get variables from Hoge class.
    "getVariableNames" is my hope method.
    Code (csharp):
    1.  
    2. Hoge hoge = GetComponent<Hoge>();
    3. string[] names = hoge.getVariableNames();
    4. foreach (string name in names) {
    5.     Debug.Log("variable name is :" + name);
    6. }
    7.  
    result that I hope
    Code (csharp):
    1.  
    2. variable name is :a
    3. variable name is :b
    4. variable name is :c
    5.  
    Is there the method for using variable name as variable in C#.
    I also wanna use variable name as variable.
    Unknown.NameToVariable is the method I find for.

    like that:
    Code (csharp):
    1.  
    2. foreach (string name in names) {
    3.     Unknown.NameToVariable(hoge, name) = 1;
    4. }
    5.  
    Later, the hoge instance's a, b and c will be set 1 :D
     
  2. Julien G.

    Julien G.

    Joined:
    Oct 20, 2011
    Posts:
    25
    Hi shinriyo,

    you can do that with reflexion :

    Code (csharp):
    1.  
    2. foreach (System.Reflection.MemberInfo m in typeof(Hoge).GetMembers())
    3.             Debug.Log(m.Name);
    4.  
    Julien G.
     
  3. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    Hi JulienG

    Thank you.
    I accessed Hoge class's members.

    I'd like to assign value to these members of instance.
    Is it realizable?
    like that
    Code (csharp):
    1.  
    2. Hoge hogeInstance;
    3. hogeInstance = this.gameObject.GetComponent<MegaStretch>();
    4.  
    5. foreach (System.Reflection.MemberInfo m in typeof(Hoge).GetMembers())
    6.    hogeInstance /* something */ m.Name  = 1;
    7. }
    8.  
    I want to assign 1 to hogeInstance's member(m.Name) .

    shinriyo
     
  4. shinriyo_twitter

    shinriyo_twitter

    Joined:
    Aug 12, 2011
    Posts:
    328
    And I also get value already assigned in hogeInstance

    Code (csharp):
    1.  
    2. Hoge hogeInstance;
    3. hogeInstance = this.gameObject.GetComponent<MegaStretch>();
    4.  
    5. foreach (System.Reflection.MemberInfo m in typeof(Hoge).GetMembers())
    6.    Debug.Log(?????);
    7. }
    8.  
    ????? is my hoping code converting m.Name to hogeInstance's variable.
     
unityunity