[Solved] How to use a variable for transformation target

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:

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.

ContentOfNameVariable.transform.Rotate(direction, degree)

Thank you for your advice!

EDIT: I use the following method to connect my Object with the script:
4464505--409891--upload_2019-4-24_12-26-18.png

Inside the script I declare

    public Text text;
    public GameObject table;
    public GameObject cube;

And the rotation:

    public void rotation(Vector3 direction, float degree)
    {
        table.transform.Rotate(direction, degree);
    }

4464505--409891--upload_2019-4-24_12-26-18.png

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.

1 Like

does exactly this, you give it the name of the object and it gives you the gameobject with that name. However, I would advise against this approach at runtime due to the heavy memory footprint it causes.

1 Like

Thank you very much for your replies. I ended up with using dropdown.value.