Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question other objects position in this objects script

Discussion in 'Scripting' started by Lockinto, Dec 24, 2023.

  1. Lockinto

    Lockinto

    Joined:
    Feb 11, 2023
    Posts:
    5
    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!
     
  2. _geo__

    _geo__

    Joined:
    Feb 26, 2014
    Posts:
    1,410
    Kurt-Dekker likes this.
  3. Lockinto

    Lockinto

    Joined:
    Feb 11, 2023
    Posts:
    5
    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.
     
  4. Lockinto

    Lockinto

    Joined:
    Feb 11, 2023
    Posts:
    5
    I also need to have that as a value that I can use in the code.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,410
    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://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    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.
     
  6. Lockinto

    Lockinto

    Joined:
    Feb 11, 2023
    Posts:
    5
    thanks im gonna read through all this, I will tell you if I need any more help
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,410
    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://forum.unity.com/threads/using-code-tags-properly.143875/

    - 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.
     
  8. Lockinto

    Lockinto

    Joined:
    Feb 11, 2023
    Posts:
    5
    I got it to work, thanks!
     
    Kurt-Dekker likes this.