"Vector 3" saving players x, y, and z position [FIXED]

Hey guys, I want to save the players x, y, and z every time the player saves the game.

so pseudo code wise:

if(player presses x)
gameControl.spawnpoint =  players.transform.position;

//when the game loads in the start function it will
playerSpawnPoint = gameControl.spawnpoint

public class GameControl : MonoBehaviour {


    //spawn point
    public const PLAYER_SPWAN_POINT = "Player Spawn Point";//game object name that player spawns from at start of level
    public Vector3 spawnPoint;

//the top class is what I have as a set up

//so lets presume I have a class called "SpawnPoint" script saved to the player how will i be able to take its position and save it into gameControl.spawnpoint;

Sorry if that sounds naive, I know how to do it with like a 2D game, such as Action Script 3, but it seems difficult to implement in a 3D game.

I know I will need to use the vector3 variable…

I dont understand, you have everything you would need in that script(though not written correctly). player.transform.position will give you a Vector3 of that players position in the world.

savedSpawnPoint = player.transform.position;

Are you asking how you would update the players position from a stored Vector3?

player.transform.position = savedSpawnPoint;

Hey. thanks for the comment.

I am asking, how can i store the players position in to a variable.

For example, lets say I attach a script to the player.

I make a variable called “private Vector3 playersPos;”

Now I want to store the players position into this variable.

playerPos = this.tansform.position;

However, this is not possible, so is there another solution?

That is possible. If the script that you are calling

playerPos = this.tansform.position; //BTW if this is the code you are using, youve spelled transform wrong

is attached to the player object, storing the position of the player is just like youve got there.

    private Vector3 playerPosition;

    void Start(){
        playerPosition = this.transform.position;
        Debug.Log("X:" + playerPosition.x + " Y:" + playerPosition.y + " Z:" + playerPosition.z);
    }

If you are trying to store the player position FROM the player INTO another script containing the “playersPos” variable, you need to set the variable to public, not private

THANK YOU!

This is what I was looking for :smile:

I understand, if the script is attached to the object, you can use “this”

Thanks, this made it very clear with the example.

Much appreciated b1gry4n

no, you can always ‘this’, but:
‘this’ refers to the script body in which the ‘this’ is written
if you have

public class GameControl : MonoBehaviour {
  ....
  this.anythingHere
}

then the ‘this’ refers to the GameControl. Since monobehaviours have a transform property, you can call this.transform to get the transform of the gameObject attatched