-2
    public class player_movement : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform position;
    void Start()
    {
        position = GetComponent<Transform>();
    }

how do I acsess the position variable in another script

derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

0

There is no reason for any of this at all.

Your component player_movement is of type MonoBehaviour which is a Behaviour which is a Component and therefore already has the inherited property transform returning the Transform reference if the GameObject your component is attached to.

So whoever has a reference to your component immediately also has access to its .transform.position anyway.


As to how get that reference, there are thousands of different ways. The most typical ones

public player_movement playerMovement;

simply drag and drop it in vis the Inspector.

On another component attached to the same GameObject use

var playerMovement = GetComponent<player_movement>();

or if there is only one in the scene anyway

var playerMovement = FindObjectOfType<player_movement>();

either way in the end as said you can simply use

var playerTransform = playerMovement.transform;

and do with it whatever you want like accessing its position

var playerPosition = playerTransform.position;
derHugo
  • 83,094
  • 9
  • 75
  • 115