How get position from a game object?
Im trying to make a cam follow a gameobject, and I don't know how get the position of the object. helpme please
can i store the value on a var... something like this ?
myvalue = GameObject.FindGameObjectWithTag("Your_Tag_Here").transform.position;
Answer by jimmycrazyskills · Jan 06, 2014 at 08:31 PM
You can find the object by the object tag.
GameObject.FindGameObjectWithTag("Your_Tag_Here").transform.position;
Or you can find the object by the name of the object.
GameObject.Find("Your_Name_Here").transform.position;
Hope that works ;)
But what do I actually need to do to use that line of code?
The script is telling me something is wrong by showing the little red under markings on this code if I simply insert into the Start function of my script as is, so clearly there needs to be other stuff setup around this line of code to get it to work.
Can you tell me what that other stuff is please?
If you need help post your current script so someone can help you.
$$anonymous$$y script is literally just what is above (it's a new script with that line of code pasted into the start function to find the position of the other object). I haven't added anything else yet. The point is that code doesn't work without something else that has not been mentioned above, something that's obviously been taken for granted and assumed everyone knows to do already by the original poster, and I'm just trying to figure out what "obvious" thing it is that has been omitted so I can add it and get it to work properly.
Answer by Waz · Aug 16, 2011 at 06:08 AM
https://docs.unity3d.com/Manual/ControllingGameObjectsComponents.html
Read The Fine Manual.
What's the point in having a help forum to ask questions for issues you can't resolve if people like you just tell the stumped people to go read the manual? Do you seriously think they're so dumb that they didn't try more than one way to fix the issue, of which one of them was to come to the help forum and ask for help from people who have more experience and knowledge than they do and might want to use that to help them? The point is the manual often doesn't explain things clearly enough or use an example that the person asking in here has been able to apply in their specific situation, so they're asking for advice from the community. And then there's this too: https://docs.unity3d.com/410/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Answer by krewnel · Jan 06, 2014 at 07:05 PM
If the script you are using is atached to the object for which you want the position it is as simple as
Vector3 bar = transform.position;
otherwise you need to get an instance of the object of which you want the position:
foo = Object_In_Scene.GetComponent(); Vector3 bar = foo.position;
It's all a matter of relativity.
"Read The Fine Manual." ~Waz
Answer by Victor2006 · Mar 11, 2017 at 07:16 PM
Hello @gabosuelto! if you want to make the MainCamera follow a GameObject, you can write a C# Script, but the simplest thing to do is drag your MainCamera onto the GameObject.
Well then try to drag your $$anonymous$$ainCamera onto a GameObject, and if you have a game in which you rotate a ball, make that ball rotate.