- Home /
GameObject.Find("In-Game-Object") Position
Hi,
I need to know if I need to make seperate script (to use transform.position) and then attach it to each game object to know about the individual positions.
public static Vector3 Object_1, Object_2, Object_3 Object_4, Object_5 ;
// Use this for initialization void Start () { if (GameObject.Find("Object_1")) { Object_1 = transform.position; } if (GameObject.Find("Object_2")) { Object_2 = transform.position; } if (GameObject.Find("Object_3")) { Object_3 = transform.position; } if (GameObject.Find("Object_4")) { Object_4 = transform.position; } if (GameObject.Find("Object_5")) { Object_5 = transform.position; } }
The above is in one script file, attached to each Object, [1,5]. The intention is to know about each objects individual position.
When I go to Debug.Log(Object_2) I see that it is either 0,0,0 (not true) or overwritten..However, Logging, Object_1 is fine...Is it true that I need to make seperate script for each game object?
I want to find an object on a specific position. How can I do that?
Answer by Adrian Love · Jun 15, 2010 at 03:05 AM
Okay, So what objects are in your hierarchy?
I read your code like this:
When this object is loaded look for (Game)Objects 1 through 5 and save their positions into Vector3 objects labeled "Object_1" through "Object_5".
If you'd like to access the transform try something like this:
GameObject.Find("Object_1").transform.position;
Read more about selectors here: http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html
Each GameObject has a Transform component automatically: http://unity3d.com/support/documentation/ScriptReference/Transform.html
Also, you might want to try and use tags if you find yourself accessing a lot of the same type of object. read more about them here: http://unity3d.com/support/documentation/Components/Tags.html
Good luck with whatever you're doing. If you want more specific information try expressing what the overall goal is next time. :)
The Object1,2,3,4,5 are in my heirarchy. I notice that when I click on them, they all have their own distinct positions. I need to have Object1's position in the Object1 Vector3 Varaible. and so on and so forth.
The snippet that I posted--the script is attached to each of my game objects.
I don't know what to tag $$anonymous$$y Game Objects as, they are geometric planes, which never move.
I am getting the position of one of the game objects, if I could get the rest without attached seperate script files, that'd be great. THat is my goal here, not using seperate script files to find positions of invidiudal game obejcts.
I'm just curious as to why you'd like to store the same information twice in a single GameObject. Other scripts will be able to access the transform component of this game object. So if you were in another script you could just use "GameObject.Find("Object_1").transform.position;" and that would return the objects' position...
All right! Thanks a lot, I didn't think about just accessing the position directly in the script that I needed it in, Duh!...Thanks! =)
Answer by TSSTUDIOS · Feb 21, 2015 at 03:03 AM
here
if(star1position){transform.position = GameObject.Find("Star1Position").transform.position;}
in this case i got a bool but u can simply
transform.position = GameObject.Find("Star1Position").transform.position;
Your answer
![](https://koobas.hobune.stream/wayback/20220613062134im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI Box not showing up after object clicked 1 Answer
How to add component to a child of gameobject 3 Answers
selecting and organising objects in editor 0 Answers
Deactivating/activating gameobjects on button press 1 Answer
how to make public variables unique to the gameobject it is attached to? 1 Answer