other objects position in this objects script

i am new to unity and I need some help finding the x and z of another game object I have this code:

public GameObject wantedLegPosition;
private float wantedX;
private float wantedZ;

and I want the selected game objects (wantedLegPosition) x and z coordinate, and I want that to be wantedX and wantedZ, if you can help, that would be nice, thanks!

Hi, welcome to the Unity Forum.
Please go read this: Unity - Scripting API: Transform.position

1 Like

im sorry but this isnt what im looking for, I wanted to get the location of another game object, that could be changed by it being a public object not move my object, that part I can do on my own.

I also need to have that as a value that I can use in the code.

That actually IS what you want. There is no other way to find a position of a GameObject.

Then you ALSO need this, in addition to what geo_ showed you:

Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:

https://discussions.unity.com/t/833085/2

https://discussions.unity.com/t/839310

It isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

thanks im gonna read through all this, I will tell you if I need any more help

Excellent!! Remember you don’t need to tell me, but you do need to communicate technical issues clearly.

I like this framework. You may find it useful too.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • Do NOT attach entire scripts to your post.
  • ONLY post the relevant code, and then refer to it in your discussion.

I got it to work, thanks!

1 Like