transform.position do not update

Hey guys

I am new to unity it is my first project, I am doing a simple player movement with arrow keys, and I want to make a button when pressed it resets the player to a certain position 0,0,0 . or in my case I want to save a certain position and then reset to it. I have attached the camera to the player and made move camera script and the player moves just fine but when I press the button to reset the position the transform.position do not make any changes in the game. my code is as the following

public GameObject mainCamera;
public GameObject DeactivateEgoButton;
public GameObject LoadCameraPositionButton;
public GameObject SaveCameraPositionButton;
public GameObject Player;

private Vector3 savedPlayerPosition;
private Quaternion savedPlayerRotation;
private Quaternion savedCameraRotation;

// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    
}

public void activateCamera()
{
    Camera.main.gameObject.SetActive(false);
    mainCamera.SetActive(true);
    DeactivateEgoButton.SetActive(false);
    SaveCameraPositionButton.SetActive(true);

    Debug.Log("Camera Activated");

}

public void saveCameraPosition()
{
    // we have to save position of the player that is why we use .parent
    savedPlayerPosition = mainCamera.transform.parent.position;
    savedPlayerRotation = mainCamera.transform.parent.rotation;
    savedCameraRotation = mainCamera.transform.rotation;
    LoadCameraPositionButton.SetActive(true);
    Debug.Log(savedPlayerPosition);
    mainCamera.transform.parent.position = new Vector3(0, 0, 0);

}

public void loadCameraPosition()
{
    Debug.Log(mainCamera.transform.parent.name);
    Debug.Log(mainCamera.transform.parent.position);
    mainCamera.transform.parent.position = savedPlayerPosition;
    mainCamera.transform.parent.rotation = savedPlayerRotation;
    this.mainCamera.transform.rotation = savedCameraRotation;
    mainCamera.transform.parent.position = new Vector3(0,0,0);
    Debug.Log(mainCamera.transform.parent.position);
    //if(!mainCamera)
    //{
    //    Camera.main.gameObject.SetActive(false);
    //    mainCamera.SetActive(true);
    //    DeactivateEgoButton.SetActive(false);
    //    SaveCameraPositionButton.SetActive(true);


//}

}

within the picture the buttons for load camera position when pressed is supposed to call loadCameraPosition to reset camera position, but it does not work

Thanks in advance

I notice you are setting the position twice without doing anything with it in between. Line 44 it’s set to savedPlayerPosition but on line 46 you set it to new Vector3(0,0,0). Is it possible you meant to set savedPlayerPosition to new Vector3(0,0,0) on line 46?

As HellsHands already mentioned in the comment above, have a closer look at your “loadCameraPosition”. You set the camera’s parent position twice

 public void loadCameraPosition()
 {
     // [ ... ]
     mainCamera.transform.parent.position = savedPlayerPosition;
     mainCamera.transform.parent.rotation = savedPlayerRotation;
     this.mainCamera.transform.rotation = savedCameraRotation;
     mainCamera.transform.parent.position = new Vector3(0,0,0);
     // [ ... ]

Note the first and last line here are both setting the position of the camera parent. So your first line is meaningless since you overwrite the position again to (0,0,0).

So this looks like a copy & paste error. You probably want to remove that last line.

I tried this script (my script) and it works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PosSave : MonoBehaviour {

	public Transform player;
	public Vector3 PlayerPos;
	public Quaternion PlayerRoto;

	// Use this for initialization
	void Start () 
	{
		SavePOS ();
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKeyDown (KeyCode.P))
		{
			SavePOS ();
		}

		if(Input.GetKeyDown (KeyCode.L))
		{
			LoadSavePOS ();
		}
	}

	public void SavePOS ()
	{
		PlayerPos = player.position;
		PlayerRoto = player.rotation;
	}

	public void LoadSavePOS ()
	{
		player.position = PlayerPos;
		player.rotation = PlayerRoto;
	}
}

[178558-screenshot-7.png|178558]