help with position of objects

hi folks,

i have a script which creats different gameobjects at the start of the game. (the information what gameobjects and where to create them is coming from another program / a textfile).
the goal is do be able to move those objects and get the NEW coordinates printed in the same (now updated) txtfile.

right now i am trying to access the created gameobjects. (which are only cubes so far)
how can i call the different cubes? Gameobject.Find does not seem to work

    public void Button_export()
    {


        a = GameObject.Find("cube");
        pos = transform.position;
        Debug.Log("pos is " + pos);

    }
}

the code of creating gameobjects looks like this (i want to call the position of those cubes)

 if (itemLabel.Equals("Quelle"))
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cube.transform.position = new Vector3(x, y, z);
            }

         else if (itemLabel.Equals("Senke"))
           {
              GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
               cube.transform.position = new Vector3(x, y, z);
            }

You don’t name it cube you actually just give it a name in the script. If you do something like cube.transform.name = “Fart” after instantiating/creating. Then running GameObject.Find(“Fart”) will find that gameObject;

it works like u say. and thats very lovely.
but i think it is a bad idea to rename all the objects i want to create. (as i need to call al the names seperatly)
i think it is a better way to make all the gameobjects i want to create childs of a parent
i tried it like this :

                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                cube.transform.position = new Vector3(x, y, z);
                cube.transform.parent = newPARENT.transform;

but thats not working as the cube wont accept the parent

Hello Pandoflo!

A good way of doing what you want would be to assign all your cubes a single tag when you create them. Then, in your script, you can search all the game objects, in the scene, that have that specific tag.

To do that, you can use GameObject.FindGameObjectsWithTag. It returns the found game objects into an array. Let’s say your tag is named cubesGO:

GameObject[] cubesArray;

void Update ()
    {
        cubesArray = GameObject.FindGameObjectsWithTag("cubesGO");

        foreach (GameObject cube in cubesArray)
        {
            //Do what you want
        }
    }

In this case, the foreach loop is used to cycle through all your cubes. But you can use the array as you wish!

seems like a good idea but the same with transform.paren
i cant set a tag

used

cube.tag = “newTag”;

i get an error

Tag: P is not defined.
UnityEngine.GameObject:set_tag(String)
txtEinlesen:Start() (at Assets/txtEinlesen.cs:44)

You have to manually create the tag in the Editor, then assign it through the script.

To create a tag, select any game object in your Hierarchy, then at the top of the Inspector, you have a Tag dropdown. Click on it, then click on “Add tag…” at the bottom.

Why don’t you just keep the references to them as soon as you instantiate the objects?

Methods like ‘Instantiate’ (in your case it’s ‘CreatePrimitive’) always return the created instance. Add the instance to a collection (e.g. a List). Once you’re done creating all the objects, you can iterate the collection and do further processing.