- Home /
Position of a GameObject
Hello,
I was wondering to find the position of a gameobject with a certain tag on the 'x' co-ordinate. I wrote a code and found that it did not give the answer I was expecting.
var player = GameObject.FindGameObjectsWithTag ("Player");
var dis : float;
function Update ()
{
dis = player.transform.position.x;
print(dis);
}
Request:
Is my code right, if not how could I find a position of a gameobject which is not attached onto a script.
Thank you very much.
is the GameObject.FindGameObjectsWithTag line of code in your variable declarations or is it inside of a method?
WEll the code I posted above is the entire script. I was testing with it. $$anonymous$$y sole aim is to find the position of a gameobject through script attached to another gameobject. Say I have enemy prefab I wish to find it's position.
COUld you please help me on this.
Well if you are instantiating a prefab you can always just ask the position of your clone. Otherwise try GameObject.FindWithTag("Player"), since you are looking for just one player (i assume).
How do I ask the position of my clone? That is where I think I am lacking.
Answer by ks13 · Dec 09, 2011 at 09:18 AM
Actualy, what you're caling is GameObject.FindGameObject*s*WithTag ("Player"). With an "s" at the end of Objects. That means your player is an array even if there is ony one object. So you should do dis = player[0].transform.position.x;
How do I ask the position of my clone? That is where I think I am lacking.
I changed and it works but I want to call the position of it's clone? That is where I am not sure.
Answer by hatzalex · Dec 09, 2011 at 06:51 PM
Asking the position of a instatiated clone is pretty simple.
var clone : GameObject; var player: GameObject;
clone = Instantiate(player,transform.position,transform.rotation);
print (clone.transform.position);
The last line gets the current position of the clone.
Also take a look at instantiate for more info:
Edit: Btw, you said it does not give the answer you are expecting, but what answer are you expecting?
var clone : GameObject; var player : GameObject;
Are both of these the same Prefab?
YOu will get the print correctly but I think as you refer the prefab on the project folder it gets its position and not the it's virtual clone.
You store the Intatiated player into the clone variable. That means that "clone" is empty until you store the Instantiated player variable in it. "Clone"is literally a clone of the player gameobject.
In the inspector you assign the player gameobject to the player var, but you leave the clone variable empty, since you must store the Instantiated player in it.
When you ask the position of the clone, you actually ask the position of the player you just Instantiated.
Not sure he understood that...
Let's explain it this way :
"player" is the model of what you want to copy
"clone" is the adress of the copied object
the same way you put an int into a variable, "clone" contains the newly created object object, without it you don't know where the information fo the clone is stored and can't use it.
Not much better of an explanation but hope you understood how it works now.
Your answer
Follow this Question
Related Questions
Instantiate as a child at position 2 Answers
Getting an Objects position once 2 Answers
How To Move GameObjects In Fixed Path? 1 Answer
How to tell if two blocks are right next to each other?(2D) 1 Answer