Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by SenorGoatMan · Aug 06, 2014 at 10:31 PM · c#transformserialization

Transform object issues

Hello guys, I've encountered an issue I can't figure out; How to move a player to their last saved location upon load. I have three scripts written in C# whose purpose is to accomplish this:

Pause, a script to perform a save/load function

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class Pause : MonoBehaviour {
 
     public static List<Game> savedGames = new List<Game>();
 
     public static bool paused = false;
     public Texture saveButton;
     public Texture loadButton;
     public static bool saving = false;
 
     void Update(){
 
         if(Input.GetKeyUp (KeyCode.P)){
 
             paused = !paused;
 
         }
     }
 
     void OnGUI(){
 
         if(paused && GUI.Button(new Rect(525, 250, 256, 64), saveButton, GUIStyle.none)){
 
             Pause.savedGames.Add(Game.current);//Add a save file
             BinaryFormatter bf = new BinaryFormatter();//Prepare serialization tool
             FileStream file = File.Create ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd");//Defining save file path, and name the save file there
             bf.Serialize(file, Pause.savedGames);//Use the prepared binary formatter to serialize the designated classes
             file.Close();//Close the file (duh)
             Debug.Log ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd");
             saving = true;
 
         }
         if(paused && GUI.Button(new Rect(525, 350, 256, 64), loadButton, GUIStyle.none)){
 
             if (File.Exists ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd")) {//Make sure there is a saved game
                 
                 BinaryFormatter bf = new BinaryFormatter ();//Prepare deserialization tool
                 FileStream file = File.Open ("C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd", FileMode.Open);//Find file
                 Pause.savedGames = (List<Game>)bf.Deserialize (file);//Use deserialization tool
                 file.Close ();//Close the file (duh)
                 Debug.Log ("Loaded file  C:/Users/shane/Documents/FourKingdoms/Assets/savedGame.gd");
                 Player.SetLoc ();
                 
             } else {
                 
                 Debug.Log ("No save file found");
                 
             }
         }
     }
 }

Game, a script to hold variables that will be saved/loaded:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Game{
     
     public static Game current;
     public static Vector3 pLoc;
     /*public static float xLoc;
     public static float yLoc;
     public static float zLoc;*/
 
     void Update(){
 
         if(Pause.saving){
 
             /*xLoc = Player.ReportXLoc();
             yLoc = Player.ReportYLoc();
             zLoc = Player.ReportZLoc();*/
             pLoc = Player.GetLoc();
             Pause.saving = false;
 
         }
     }
 }

And finally player, which is attached to(you guessed it) the player.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     
     public static GameObject player;
     public static float xLoc;
     public static float yLoc;
     public static float zLoc;
     public static Vector3 playerLoc;
     // Use this for initialization
     void Start () {
     
         player = GameObject.Find ("First Person Controller");
 
     }
     public static Vector3 GetLoc(){
 
         playerLoc = player.transform.position;
         return playerLoc;
 
     }
 
     public static void SetLoc(){
 
         player = GameObject.Find ("First Person Controller");
 
         player.transform.position = new Vector3 (Game.pLoc.x, Game.pLoc.y, Game.pLoc.z);
     
     }
 }


I am thoroughly stumped, any help would be greatly appreciated, thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SenorGoatMan · Aug 07, 2014 at 02:17 AM

Well, sir, it is plain to see for even the blindest of fools that you must serialize the x, y and z values of you player's location upon saving and then deserialize them upon loading.

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 SenorGoatMan · Aug 07, 2014 at 02:18 AM 0
Share

Gee, thanks mister!

avatar image SenorGoatMan · Aug 07, 2014 at 02:19 AM 0
Share

Stop talking to yourself on a public forum, people might think we're weird

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

21 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

Related Questions

Picking up an Object and locking its position relative to parent. 1 Answer

Javascript to C# 1 Answer

Moving an object away from collider 1 Answer

GUI.Button child of the camera . 0 Answers

How do I serialize a class? 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