Search Unity

Resolved transform.position does not work

Discussion in 'Scripting' started by Caljay, Jul 31, 2020.

  1. Caljay

    Caljay

    Joined:
    Mar 27, 2020
    Posts:
    10
    I've been trying to make my own save and load features for my game. I got everything working until the last few lines


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity;
    5. using System.Security.Cryptography;
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.     public void SavePlayer ()
    10.     {
    11.         SaveSystem.SavePlayer(this);
    12.       //  Debug.Log("SaveSYstem");
    13.  
    14.     }
    15.  
    16.     public void LoadPlayer()
    17.     {
    18.  
    19.      
    20.        
    21.         PlayerData data = SaveSystem.LoadPlayer();
    22.  
    23.         Debug.Log($"LoadingSystem {data.position[0]}, {data.position[1]}, {data.position[2]}");
    24.  
    25.        
    26.         Vector3 position;
    27.         position.x = data.position[0];
    28.         position.y = data.position[1];
    29.         position.z = data.position[2];
    30.  
    31.         transform.position = position;
    32.      
    33.  
    34.     }
    35.  
    36.  
    37.  
    38. }
    39.  
    I've tried looking up solutions online but could not find any.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    What isn't working? Are you getting an error? What are you expecting to happen? What is happening instead?
     
  3. Caljay

    Caljay

    Joined:
    Mar 27, 2020
    Posts:
    10
    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.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    One possibility is you have some other script or an Animator that is moving the player and overwriting whatever changes come from this script.
     
  5. Caljay

    Caljay

    Joined:
    Mar 27, 2020
    Posts:
    10
    would a simple movement script overwrite it?
    also there is no compiler errors which makes me think that it should work
     
    Last edited: Jul 31, 2020
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    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.
     
  7. Caljay

    Caljay

    Joined:
    Mar 27, 2020
    Posts:
    10
    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()
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Again, it depends on how your movement script works. if you share your movement code maybe us forum denizens could offer some pointers.
     
  9. Caljay

    Caljay

    Joined:
    Mar 27, 2020
    Posts:
    10
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6.  
    7. {
    8.  
    9.     public CharacterController controller;
    10.  
    11.     public float speed = 10f;
    12.     public float gravity = -10f;
    13.     public float jumpHeight = 5f;
    14.  
    15.     public Transform groundCheck;
    16.     public float groundDistance = 0.4f;
    17.     public LayerMask groundMask;
    18.  
    19.     Vector3 velocity;
    20.     bool isGrounded;
    21.  
    22.     void Update()
    23.  
    24.     {
    25.  
    26.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    27.  
    28.  
    29.  
    30.         if (isGrounded && velocity.y < 0)
    31.         {
    32.  
    33.             velocity.y = -2f;
    34.  
    35.         }
    36.  
    37.      
    38.         float x = Input.GetAxis("Horizontal");
    39.  
    40.         float z = Input.GetAxis("Vertical");
    41.  
    42.  
    43.  
    44.         Vector3 move = transform.right * x + transform.forward * z;
    45.  
    46.  
    47.  
    48.        
    49.                       controller.Move(move * speed * Time.deltaTime);
    50.  
    51.  
    52.  
    53.                                if (Input.GetKeyDown(KeyCode.Space))
    54.                                {
    55.  
    56.                                    velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    57.  
    58.                        }
    59.  
    60.  
    61.  
    62.                                 velocity.y += gravity * Time.deltaTime;
    63.  
    64.  
    65.                
    66.         controller.Move(velocity * Time.deltaTime);
    67.                      
    68.        
    69.     }
    70.  
    71. }
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Last edited: Jul 31, 2020
    Bella_1303, XaviV, tonnykwon and 9 others like this.
  11. Caljay

    Caljay

    Joined:
    Mar 27, 2020
    Posts:
    10
    that worked!! thank you alot
     
  12. AstroMusiCoder_85

    AstroMusiCoder_85

    Joined:
    Sep 6, 2021
    Posts:
    1
    I am getting a problem with the transform.position.
     

    Attached Files:

  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,474
    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.
     
  14. salvado89

    salvado89

    Joined:
    Aug 25, 2021
    Posts:
    1
    It's easy.
    Code (CSharp):
    1. characterController.enabled = false;
    2. transform.position = position;
    3. characterController.enabled = true;
    4.  
    Hard in invisible problem.
     
  15. Etonix

    Etonix

    Joined:
    Sep 21, 2018
    Posts:
    203