transform.position does not work

I’ve been trying to make my own save and load features for my game. I got everything working until the last few lines

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity;
using System.Security.Cryptography;

public class Player : MonoBehaviour
{
    public void SavePlayer ()
    {
        SaveSystem.SavePlayer(this);
      //  Debug.Log("SaveSYstem");

    }

    public void LoadPlayer()
    {

      
       
        PlayerData data = SaveSystem.LoadPlayer();

        Debug.Log($"LoadingSystem {data.position[0]}, {data.position[1]}, {data.position[2]}");

       
        Vector3 position;
        position.x = data.position[0];
        position.y = data.position[1];
        position.z = data.position[2];

        transform.position = position;
     

    }



}

I’ve tried looking up solutions online but could not find any.

What isn’t working? Are you getting an error? What are you expecting to happen? What is happening instead?

1 Like

i have this connect to my player and have buttons that when pressed load the SavePlayer() and LoadPlayer()
the save player works but when i try to hit my load button it doesn’t move my player. I expected it to move my player and it doesn’t work. I check and it loads the saved position too.

One possibility is you have some other script or an Animator that is moving the player and overwriting whatever changes come from this script.

would a simple movement script overwrite it?
also there is no compiler errors which makes me think that it should work

Maybe? Depends on the code. I assure you if you DebugLog the position right after you change it in this script it will have been changed. Something else is overwriting it later.

i commented my movment script out and tried it out and that save and load feature worked!
now how do i add it back without overwriting the load Player()

Again, it depends on how your movement script works. if you share your movement code maybe us forum denizens could offer some pointers.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControl : MonoBehaviour

{

    public CharacterController controller;

    public float speed = 10f;
    public float gravity = -10f;
    public float jumpHeight = 5f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    void Update()

    {

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);



        if (isGrounded && velocity.y < 0)
        {

            velocity.y = -2f;

        }

      
        float x = Input.GetAxis("Horizontal");

        float z = Input.GetAxis("Vertical");



        Vector3 move = transform.right * x + transform.forward * z;



       
                      controller.Move(move * speed * Time.deltaTime);



                               if (Input.GetKeyDown(KeyCode.Space))
                               {

                                   velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

                       }



                                velocity.y += gravity * Time.deltaTime;


               
        controller.Move(velocity * Time.deltaTime);
                      
       
    }

}

Ah you are using a CharacterController. I believe this is relevant for your issue:

Solutions:

14 Likes

that worked!! thank you alot

I am getting a problem with the transform.position.


It tells you what you’re doing wrong in that you’re assigning something to Transform.position.x when you cannot. Maybe you should state what you want but also do it on your own thread; the above thread has nothing to do with your problem.

It’s easy.

characterController.enabled = false;
transform.position = position;
characterController.enabled = true;

Hard in invisible problem.

Been pulling my hair out for ages on this one. Thanks!