Search Unity

[Solved] How to use a variable for transformation target

Discussion in 'Scripting' started by scythwolf, Apr 24, 2019.

  1. scythwolf

    scythwolf

    Joined:
    Dec 30, 2018
    Posts:
    49
    Hello!

    I ran into a problem lately and couldn't find any solution using google. Sorry if this is a noob questen... but as said, couldn't find anything anywhere not even a workaround.

    In my app I have a dropdownbox which i populate with Gameobjects using tags. If an Object is chosen using the dropdownbox you can rotate it. Now I have a problem with the rotation target:
    Code (CSharp):
    1. table.transform.Rotate(direction, degree)
    Instead of the using something static "table." I need this to be more dynamic. I can store the name of the chosen gameobject when I populate the dropdownbox but how can I call it with this variable? In this example NameVariable stores the name of the game object to be rotated.
    Code (CSharp):
    1. ContentOfNameVariable.transform.Rotate(direction, degree)
    Thank you for your advice!

    EDIT: I use the following method to connect my Object with the script:
    upload_2019-4-24_12-25-22.png

    Inside the script I declare
    Code (CSharp):
    1.     public Text text;
    2.     public GameObject table;
    3.     public GameObject cube;
    And the rotation:
    Code (CSharp):
    1.     public void rotation(Vector3 direction, float degree)
    2.     {
    3.         table.transform.Rotate(direction, degree);
    4.     }
     

    Attached Files:

    Last edited: Apr 24, 2019
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Keep a copy of the list or array that you use to populate the dropdown's data with, and use dropdown.value to receive an index back to your list/array. Then you know what object the target of your rotation should be.
     
    scythwolf likes this.
  3. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    scythwolf likes this.
  4. scythwolf

    scythwolf

    Joined:
    Dec 30, 2018
    Posts:
    49
    Thank you very much for your replies. I ended up with using dropdown.value.