Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ThePizzaGuy · Dec 03, 2019 at 02:23 AM · scripting problem

Why doesn't my character teleport to saved location when loading?

I am implementing a load game method on my player script, I realize that this should probably be in a game manager script but oh well...

The only data that gets stored is a boolean value, an integer that represents which spawn point they should spawn at, the position of that spawn point, and the players inventory. I know this data is being saved and loaded because the inventory is re-populated when I start the game, and the output of the below debug calls is (Also I left out the code for the inventory population as not to clutter up the problem code):

LOADING GAME:

Save Spot: 2

Teleporting to: 9.816902 -0.4699752 22.40802 From:

Current Position: 19.99548 1.585351 -20.008

New Position: 9.816902 -0.4699752 22.40802

As you can see, the 'current position' and 'new position' are the exact same line of code, but after assigning the transform of the player to the new spot, somehow the transform is updated but the character is not. any ideas?

 private void LoadGame(PlayerData tempPlayer)
         {
             if (tempPlayer != null)
             {
                 Debug.Log("LOADING GAME:");
                 
                 // Assigning variables
                 saveSpot = tempPlayer.saveSpot;
                 level2 = tempPlayer.level;
                 string[] inventory = tempPlayer.inventory;
                 savePos = tempPlayer.position;
                 Debug.Log("Save Spot: " + saveSpot);
     
                 // Move player to save position
                 Vector3 position = new Vector3(tempPlayer.position[0], tempPlayer.position[1], tempPlayer.position[2]);
                 Debug.Log("Teleporting to: " + position.x + " " + position.y + " " + position.z + " From: ");
                 Debug.Log("Current Position: " + transform.position.x + " " + transform.position.y + " " + transform.position.z);
                 gameObject.transform.position = position;
                 Debug.Log("New Position: " + transform.position.x + " " + transform.position.y + " " + transform.position.z);
     }


Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lgarczyn · Dec 03, 2019 at 02:22 AM 0
Share

Your problem is likely elsewhere, turn off other scripts and see if it helps. Check that your loading script is actually on the player, and not on another gameobject.

avatar image Neran28 · Dec 19, 2019 at 11:08 AM 0
Share

When does your loading happen? Possible that another script is assigning a cached value to the position. Also if your loading happens before start() check if any script related to the player is defaulting the transform in start(). If thats the case you have to also update the cached / default values when restoring the position state.

avatar image akaBase · Dec 19, 2019 at 01:09 PM 0
Share

Are you trying to save the Vector3 as is? if so they aren't serializable. See these answers for a solution if so.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SeaPeaMe · Dec 03, 2019 at 04:15 AM

You might've accidentally forgot to get the saved position XD

  private void LoadGame(PlayerData tempPlayer)
          {
              if (tempPlayer != null)
              {
                  Debug.Log("LOADING GAME:");
                  
                  // Assigning variables
                  saveSpot = tempPlayer.saveSpot;
                  level2 = tempPlayer.level;
                  string[] inventory = tempPlayer.inventory;
                  savePos = tempPlayer.position;
                  Debug.Log("Save Spot: " + saveSpot);
      
                  // Move player to save position
                  Vector3 position = new Vector3(saveSpot.x, saveSpot.y, saveSpot.z); // The error might've been here, but I don't know if it is a Vector3 or an array
                  Debug.Log("Teleporting to: " + position.x + " " + position.y + " " + position.z + " From: ");
                  Debug.Log("Current Position: " + transform.position.x + " " + transform.position.y + " " + transform.position.z);
                  gameObject.transform.position = position;
                  print("Teleport Complete!"); // You can also use print instead of Debug.Log, just to make it faster to type :)
      }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePizzaGuy · Dec 05, 2019 at 12:02 AM 0
Share

Unfortunately that's not the error :( I did however switch it to Vector3 position = new Vector3(tempPlayer.position[0], tempPlayer.position[1], tempPlayer.position[2]); but that's not working either... No matter what I do I just cannot get the player to teleport.

avatar image SeaPeaMe ThePizzaGuy · Dec 17, 2019 at 10:08 AM 0
Share

$$anonymous$$aybe disable the player's rigidbody and script (If the code you've posted isn't in the same one) and then try move the player, that was the problem for me once upon a time

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

208 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotation question. 1 Answer

Player Prefs Dosen't work on android 0 Answers

get variable 2 Answers

Mouse Look Script 1 Answer

Script Crashing (Only on first run) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges