transform.position disconnects children - parent [Fixed]

[edit - changed subject to match thread better]
I have tried this both ways in C#,
Attached the FirstPersonControler in the scene to the EGO script objects listed below:

First

public GameObject _Player;

float myX;

Transform myT;

void Update ()
{

myT=_Player.transform;

myX=myT.position.x;

Debug.Log(myX);
	
}

Second

public Transform _PlayerPosition;
float myX;

void Update()
{
myX = _PlayerPosition.position.x;
Debug.Log(myX);
}

In both cases, the result back is always 0.
I realize by now I have had about 3 pots of cofee, but this is easy stuff right? What did I over look?

Any hints on this would be greatly appreciated, or if someone can create a new scene, add a basic plane, add a player prefab, add an EGO, slap the code on to a C# script and attach it to an EGO, assign the player object to the gameobject in the drop down on the code and run it, see if the debug shows the X location of the player object or not.
That would be greatly appreciated.

Well, I got it to work, however, when I change the original position, you won’t believe, what happens, the parent object assigned to GameObject moves, but all the children stay at the old location. Here is the code:

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;

public class _GameSaveLoad: MonoBehaviour {

	// An example where the encoding can be found is at
	// [url]http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp[/url]
	// We will just use the KISS method and cheat a little and use 
	// the examples from the web page since they are fully described
	
	// This is our local private members
	Rect _Save, _Load, _SaveMSG, _LoadMSG;
	bool _ShouldSave, _ShouldLoad;
	string _FileLocation,_FileName;
	public GameObject _Player;
	UserData myData;
	string _PlayerName;
	string _data;
	
	Vector3 VPosition;
	
	// When the EGO is instansiated the Start will trigger
	// so we setup our initial values for our local members
	void Start () {
		// We setup our rectangles for our messages
		_Save=new Rect(10,80,100,20);
		_Load=new Rect(10,100,100,20);
		_SaveMSG=new Rect(10,120,400,40);
		_LoadMSG=new Rect(10,140,400,40);
		
		// Where we want to save and load to and from
		_FileLocation=Application.dataPath;
		_FileName="SaveData.xml";
	
		// for now, lets just set the name to Joe Schmoe
		_PlayerName = "Joe Schmoe";
		
		// we need soemthing to store the information into
		myData=new UserData();
	}
	
	void Update () {}
	
	void OnGUI()
	{	
		if(_ShouldSave  !_ShouldLoad)
		{
			GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
			myData._iUser.x=_Player.transform.position.x;
			myData._iUser.y=_Player.transform.position.y;
			myData._iUser.z=_Player.transform.position.z;
			myData._iUser.name=_PlayerName;	
			
			// Time to creat our XML!
			_data = SerializeObject(myData);
			// This is the final resulting XML from the serialization process
			CreateXML();
			Debug.Log(_data);
		}
		else
		{
			// Show our Save Button
			_ShouldSave = GUI.Button(_Save,"Save");	
		}		
		
		if(_ShouldLoad  ! _ShouldSave)
		{
			GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
			// Load our UserData into myData
			LoadXML();
			if(_data.ToString() != "")
			{	// notice how I use a reference to type (UserData) here, you need this
				// so that the returned object is converted into the correct type
				myData = (UserData)DeserializeObject(_data);
				VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);				
				_Player.transform.position=VPosition;
				
				// just a way to show that we loaded in ok
				Debug.Log(myData._iUser.name);
			}
			// set the players position to the data we loaded
		}
		else
		{
			// Show our Load button
			_ShouldLoad = GUI.Button(_Load,"Load");	
		}
	}
	
	/* The following metods came from the referenced URL */
	string UTF8ByteArrayToString(byte[] characters) 
	{ 	
		UTF8Encoding encoding = new UTF8Encoding();
		string constructedString = encoding.GetString(characters);
		return (constructedString);
	}
	
	byte[] StringToUTF8ByteArray(string pXmlString)
	{
		UTF8Encoding encoding = new UTF8Encoding();
		byte[] byteArray = encoding.GetBytes(pXmlString);
		return byteArray;
	} 
	
	// Here we serialize our UserData object of myData
	string SerializeObject(object pObject) 
	{
		string XmlizedString = null;
		MemoryStream memoryStream = new MemoryStream();
		XmlSerializer xs = new XmlSerializer(typeof(UserData));
		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
		xs.Serialize(xmlTextWriter, pObject);
		memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
		XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
		return XmlizedString;
	}
	
	// Here we deserialize it back into its original form
	object DeserializeObject(string pXmlizedString)
	{
		XmlSerializer xs = new XmlSerializer(typeof(UserData));
		MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
		return xs.Deserialize(memoryStream);
	} 
	
	// Finally our save and load methods for the file itself
	void CreateXML()
	{
		StreamWriter writer;
		FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
		if(!t.Exists)
		{
			writer = t.CreateText();
		}
		else
		{
			t.Delete();
			writer = t.CreateText();
		}
		writer.Write(_data);
		writer.Close();
		Debug.Log("File written.");
	}
	
	void LoadXML()
	{
		StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
		string _info = r.ReadToEnd();
		r.Close();
		_data=_info;
		Debug.Log("File Read");
	}
} 

// UserData is our custom class that holds our defined objects we want to store in XML format
 public class UserData
 {
	 // We have to define a default instance of the structure
	public DemoData _iUser;
	 // Default constructor doesn't really do anything at the moment
	public UserData() { }
	
	// Anything we want to store in the XML file, we define it here
	public struct DemoData
	{
		public float x;
		public float y;
		public float z;
		public string name;
	}
}

If you create an EGO, place this on the EGO, change the GameObject to that of the first person controller, move him around a bit, save it, by using the Save button on the screen, move him about a bit some more and click on Load, this will reset him to your last saved location, the camera will fall to the ground and the player pill will have rest to the new location.

I’ll upload the package to my web server tonight and post a link, every time I try to upload ANYTHING to the threads I get “Tried to upload empty file” which is b… It is a 5 meg file in size, I tried ARJ, I tried RAR, i tried ZIP, I tried UnityPackage, nothing… so I can only assume they have me blocked from uploading files which is highly annoying right now… walks away aggravated

I really need someones help with this, anyone…
http://www.gamedevonline.com/subsite/unity/gamesavingandloadingxml.rar

That is the file that these forums refuses to let me attach to the forum, and that project shows you what is happening. Unrar it, open with Unity, run around a little bit, click Save, run around a little more, click Load and your ‘player’ will be ported back to the saved spot, but the camera will be laying on the ground bouncing. I need help stopping this behavior.

This is a core empty sandbox of mine that I use for script testing on many scales, so it is just a flate plane with 4 cubes used as walls to keep the player in the ‘arena’.

Help?

heh, fun little experiment, you have a few boolean’s in the script, after the function has been executed, have you tried setting the boolean value to it’s standard state, for example: when you load the game, you have coded a if function, just as when you type a if function in update(), if you dont set the boolean value to false again after the function is completed, it will run the function again, causing the load to happen every update, which would make the camera to bounce.
(Now Tested, i am not a pro with C#, i keep my head in with JavaScript, heh).

but i noticed when it when i put a print(); execution in between the code, the print() function was call to many times, which was positioned within the load function.

please let me know if i am on the right track.

Interesting find…

if(_ShouldLoad  ! _ShouldSave)
{
// do the load
}
else
{
// show the load
}

if(_ShouldSave  !_ShouldLoad)
{
// do the save
}
else
{
// show the save
}

When the program first loads, both states are false, that said, Load and Save show up. Now, I think I know where you are going with this, the else statements are the ones that tell it to show the buttons, since the button never shows again, that means that the values are still true so the IF will continue to loop, I hadn’t paid attention to that. Way to much cafeen most likely.

I’ll need to add in another boolean for each side of the table for has saved and has loaded so I can skip the inner save and load routiens and show the save and load buttons on that inner check based on the last state of those booleans. Just thinking out loud, should fix the problem though. So the camera bouncing is actually the method running the function again repeatedly and reloading and the positioning is getting reset on every loop.

Makes total sense, thanks for pointing that out, I’ll work on that angle.

Thanks, that was the problem, I fixed it now that I had another pair of eyes on the code. Here is the working script, works nicely too. (BTW I forgot to mention and I’ll add it to the project to post an update to later tonight on the link, is that Q turns left E turns right W/A/S/D are the normal strafe left/right and move forward back. I disconnected the mouse look feature and I am going to enable R for look up and F for look down for old school RPG style first person movements)

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;

public class _GameSaveLoad: MonoBehaviour {

	// An example where the encoding can be found is at
	// [url]http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp[/url]
	// We will just use the KISS method and cheat a little and use 
	// the examples from the web page since they are fully described
	
	// This is our local private members
	Rect _Save, _Load, _SaveMSG, _LoadMSG;
	bool _ShouldSave, _ShouldLoad,_HasSaved,_HasLoaded,_SwitchSave,_SwitchLoad;
	string _FileLocation,_FileName;
	public GameObject _Player;
	UserData myData;
	string _PlayerName;
	string _data;
	
	Vector3 VPosition;
	
	// When the EGO is instansiated the Start will trigger
	// so we setup our initial values for our local members
	void Start () {
		// We setup our rectangles for our messages
		_Save=new Rect(10,80,100,20);
		_Load=new Rect(10,100,100,20);
		_SaveMSG=new Rect(10,120,400,40);
		_LoadMSG=new Rect(10,140,400,40);
		
		// Where we want to save and load to and from
		_FileLocation=Application.dataPath;
		_FileName="SaveData.xml";
	
		// for now, lets just set the name to Joe Schmoe
		_PlayerName = "Joe Schmoe";
		
		// we need soemthing to store the information into
		myData=new UserData();
	}
	
	void Update () {}
	
	void OnGUI()
	{	
		if(_ShouldSave  !_ShouldLoad)
		{
			if(_SwitchSave)
				_HasSaved=false;
			
			if(!_HasSaved)
			{
				_SwitchSave=false;
				
				GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
				myData._iUser.x=_Player.transform.position.x;
				myData._iUser.y=_Player.transform.position.y;
				myData._iUser.z=_Player.transform.position.z;
				myData._iUser.name=_PlayerName;	
			
				// Time to creat our XML!
				_data = SerializeObject(myData);
				// This is the final resulting XML from the serialization process
				CreateXML();
				_HasSaved=true;
				Debug.Log(_data);
			}
			else
			{
				_SwitchSave= GUI.Button(_Save,"Save");
			}
		}
		else
		{
			// Show our Save Button
			_ShouldSave = GUI.Button(_Save,"Save");	
		}		
		
		if(_ShouldLoad  ! _ShouldSave)
		{
			if(_SwitchLoad)
				_HasLoaded=false;
			
			if(!_HasLoaded)
			{
				_SwitchLoad=false;
				
				GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
				// Load our UserData into myData
				LoadXML();
				if(_data.ToString() != "")
				{	// notice how I use a reference to type (UserData) here, you need this
					// so that the returned object is converted into the correct type
					myData = (UserData)DeserializeObject(_data);
					VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);				
					_Player.transform.position=VPosition;
					// just a way to show that we loaded in ok
					Debug.Log(myData._iUser.name);
				}
				_HasLoaded=true;
				// set the players position to the data we loaded
			}
			else
			{
				_SwitchLoad=GUI.Button(_Load,"Load");
			}
		}
		else
		{
			// Show our Load button
			_ShouldLoad = GUI.Button(_Load,"Load");	
		}
	}
	
	/* The following metods came from the referenced URL */
	string UTF8ByteArrayToString(byte[] characters) 
	{ 	
		UTF8Encoding encoding = new UTF8Encoding();
		string constructedString = encoding.GetString(characters);
		return (constructedString);
	}
	
	byte[] StringToUTF8ByteArray(string pXmlString)
	{
		UTF8Encoding encoding = new UTF8Encoding();
		byte[] byteArray = encoding.GetBytes(pXmlString);
		return byteArray;
	} 
	
	// Here we serialize our UserData object of myData
	string SerializeObject(object pObject) 
	{
		string XmlizedString = null;
		MemoryStream memoryStream = new MemoryStream();
		XmlSerializer xs = new XmlSerializer(typeof(UserData));
		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
		xs.Serialize(xmlTextWriter, pObject);
		memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
		XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
		return XmlizedString;
	}
	
	// Here we deserialize it back into its original form
	object DeserializeObject(string pXmlizedString)
	{
		XmlSerializer xs = new XmlSerializer(typeof(UserData));
		MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
		XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
		return xs.Deserialize(memoryStream);
	} 
	
	// Finally our save and load methods for the file itself
	void CreateXML()
	{
		StreamWriter writer;
		FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName);
		if(!t.Exists)
		{
			writer = t.CreateText();
		}
		else
		{
			t.Delete();
			writer = t.CreateText();
		}
		writer.Write(_data);
		writer.Close();
		Debug.Log("File written.");
	}
	
	void LoadXML()
	{
		StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
		string _info = r.ReadToEnd();
		r.Close();
		_data=_info;
		Debug.Log("File Read");
	}
} 

// UserData is our custom class that holds our defined objects we want to store in XML format
 public class UserData
 {
	 // We have to define a default instance of the structure
	public DemoData _iUser;
	 // Default constructor doesn't really do anything at the moment
	public UserData() { }
	
	// Anything we want to store in the XML file, we define it here
	public struct DemoData
	{
		public float x;
		public float y;
		public float z;
		public string name;
	}
}

What I just learned through this little experiment, is how Unity is placing objects, or rather moving object around. All objects start at world center of 0,0,0 and then when a new position is applied to an object in world space, Unity moves the object to world center from current location, then moves it to the new location. That is why the camera was bouncing off the terrain and disconnected from the parent. It was actually reset to world center by Unity, then they do a move child to parent, then offset the child to its original local position. This seems to be true for all objects spawned in the game world, they all start at world center, then moved to there location in the scene, and all children are spawned the same way, world center, then moved to parent object, then offset to local position.

What I want to know, is this done on every game loop for every object? If so that is major overhead.

FYI: I have updated the project that is linked, so the link has the latest build if you want a copy. I’ll leave it on my web server for a little while Until I get the time to add a WIKI entry.

wow, well, yeah, i am glad i could be of help :slight_smile:

though now when skimming trough the code again, i found that some “if” functions were a bit useless, i hope you don’t mind i removed them.

it cleans the code a bit, and when uploaded to the wiki, i will be more understandable for the young coders. ( if you know what i mean )

using UnityEngine; 
using System.Collections; 
using System.Xml; 
using System.Xml.Serialization; 
using System.IO; 
using System.Text; 

public class _GameSaveLoad: MonoBehaviour { 

   // An example where the encoding can be found is at 
   // [url]http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp[/url] 
   // We will just use the KISS method and cheat a little and use 
   // the examples from the web page since they are fully described 
    
   // This is our local private members 
   Rect _Save, _Load, _SaveMSG, _LoadMSG; 
   bool _ShouldSave, _ShouldLoad,_HasSaved,_HasLoaded,_SwitchSave,_SwitchLoad; 
   string _FileLocation,_FileName; 
   public GameObject _Player; 
   UserData myData; 
   string _PlayerName; 
   string _data; 
    
   Vector3 VPosition; 
    
   // When the EGO is instansiated the Start will trigger 
   // so we setup our initial values for our local members 
   void Start () { 
      // We setup our rectangles for our messages 
      _Save=new Rect(10,80,100,20); 
      _Load=new Rect(10,100,100,20); 
      _SaveMSG=new Rect(10,120,400,40); 
      _LoadMSG=new Rect(10,140,400,40); 
       
      // Where we want to save and load to and from 
      _FileLocation=Application.dataPath; 
      _FileName="SaveData.xml"; 
    
      // for now, lets just set the name to Joe Schmoe 
      _PlayerName = "Joe Schmoe"; 
       
      // we need soemthing to store the information into 
      myData=new UserData(); 
   } 
    
   void Update () {} 
    
   void OnGUI() 
   {    
	   
	  if (GUI.Button(_Load,"Load")) {
	    if(_SwitchLoad) 
            _HasLoaded=false; 
          
           if(!_HasLoaded) 
           { 
              _SwitchLoad=false; 
             
              GUI.Label(_LoadMSG,"Loading from: "+_FileLocation); 
              // Load our UserData into myData 
              LoadXML(); 
              if(_data.ToString() != "") 
              {   // notice how I use a reference to type (UserData) here, you need this 
                 // so that the returned object is converted into the correct type 
                 myData = (UserData)DeserializeObject(_data); 
                 VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);             
                 _Player.transform.position=VPosition; 
                 // just a way to show that we loaded in ok 
                 Debug.Log(myData._iUser.name); 
              } 
              _HasLoaded=true; 
              // set the players position to the data we loaded 
           }
	}
	
	if (GUI.Button(_Save,"Save")) {
	  if(_SwitchSave) 
		_HasSaved=false; 
          
	  if(!_HasSaved) 
	  { 
		_SwitchSave=false; 
             
		GUI.Label(_SaveMSG,"Saving to: "+_FileLocation); 
		myData._iUser.x=_Player.transform.position.x; 
		myData._iUser.y=_Player.transform.position.y; 
		myData._iUser.z=_Player.transform.position.z; 
		myData._iUser.name=_PlayerName;    
          
		// Time to creat our XML! 
		_data = SerializeObject(myData); 
		// This is the final resulting XML from the serialization process 
		CreateXML(); 
		_HasSaved=true; 
		Debug.Log(_data); 
	  }
	}
   } 
    
   /* The following metods came from the referenced URL */ 
   string UTF8ByteArrayToString(byte[] characters) 
   {     
      UTF8Encoding encoding = new UTF8Encoding(); 
      string constructedString = encoding.GetString(characters); 
      return (constructedString); 
   } 
    
   byte[] StringToUTF8ByteArray(string pXmlString) 
   { 
      UTF8Encoding encoding = new UTF8Encoding(); 
      byte[] byteArray = encoding.GetBytes(pXmlString); 
      return byteArray; 
   } 
    
   // Here we serialize our UserData object of myData 
   string SerializeObject(object pObject) 
   { 
      string XmlizedString = null; 
      MemoryStream memoryStream = new MemoryStream(); 
      XmlSerializer xs = new XmlSerializer(typeof(UserData)); 
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
      xs.Serialize(xmlTextWriter, pObject); 
      memoryStream = (MemoryStream)xmlTextWriter.BaseStream; 
      XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); 
      return XmlizedString; 
   } 
    
   // Here we deserialize it back into its original form 
   object DeserializeObject(string pXmlizedString) 
   { 
      XmlSerializer xs = new XmlSerializer(typeof(UserData)); 
      MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); 
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
      return xs.Deserialize(memoryStream); 
   } 
    
   // Finally our save and load methods for the file itself 
   void CreateXML() 
   { 
      StreamWriter writer; 
      FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName); 
      if(!t.Exists) 
      { 
         writer = t.CreateText(); 
      } 
      else 
      { 
         t.Delete(); 
         writer = t.CreateText(); 
      } 
      writer.Write(_data); 
      writer.Close(); 
      Debug.Log("File written."); 
   } 
    
   void LoadXML() 
   { 
      StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName); 
      string _info = r.ReadToEnd(); 
      r.Close(); 
      _data=_info; 
      Debug.Log("File Read"); 
   } 
} 

// UserData is our custom class that holds our defined objects we want to store in XML format 
 public class UserData 
 { 
    // We have to define a default instance of the structure 
   public DemoData _iUser; 
    // Default constructor doesn't really do anything at the moment 
   public UserData() { } 
    
   // Anything we want to store in the XML file, we define it here 
   public struct DemoData 
   { 
      public float x; 
      public float y; 
      public float z; 
      public string name; 
   } 
}

Best
Narlix, aka Theodor. :roll:

ups, :sweat_smile: i might have spoken a bit to fast there, i did not really test the code, the code that i pasted on the thread could only save and load the player once, so here is a more optimized version, i got rid of all the “if” functions, so that it is more easy and clear to see what is going on within the code, plus that this one can save and load as often as you want to. heh

using UnityEngine; 
using System.Collections; 
using System.Xml; 
using System.Xml.Serialization; 
using System.IO; 
using System.Text; 

public class _GameSaveLoad: MonoBehaviour { 

   // An example where the encoding can be found is at 
   // [url]http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp[/url] 
   // We will just use the KISS method and cheat a little and use 
   // the examples from the web page since they are fully described 
    
   // This is our local private members 
   Rect _Save, _Load, _SaveMSG, _LoadMSG; 
   bool _ShouldSave, _ShouldLoad,_HasSaved,_HasLoaded,_SwitchSave,_SwitchLoad; 
   string _FileLocation,_FileName; 
   public GameObject _Player; 
   UserData myData; 
   string _PlayerName; 
   string _data; 
    
   Vector3 VPosition; 
    
   // When the EGO is instansiated the Start will trigger 
   // so we setup our initial values for our local members 
   void Start () { 
      // We setup our rectangles for our messages 
      _Save=new Rect(10,80,100,20); 
      _Load=new Rect(10,100,100,20); 
      _SaveMSG=new Rect(10,120,400,40); 
      _LoadMSG=new Rect(10,140,400,40); 
       
      // Where we want to save and load to and from 
      _FileLocation=Application.dataPath; 
      _FileName="SaveData.xml"; 
    
      // for now, lets just set the name to Joe Schmoe 
      _PlayerName = "Joe Schmoe"; 
       
      // we need soemthing to store the information into 
      myData=new UserData(); 
   } 
    
   void Update () {} 
    
   void OnGUI() 
   {    
	   
	//***************************************************
	// Loading The Player...
	// **************************************************	   
	if (GUI.Button(_Load,"Load")) {
		
		GUI.Label(_LoadMSG,"Loading from: "+_FileLocation); 
		// Load our UserData into myData 
		LoadXML(); 
		if(_data.ToString() != "") 
		{   // notice how I use a reference to type (UserData) here, you need this 
		  // so that the returned object is converted into the correct type 
		  myData = (UserData)DeserializeObject(_data); 
		  VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);             
		  _Player.transform.position=VPosition; 
		  // just a way to show that we loaded in ok 
		  Debug.Log(myData._iUser.name); 
		} 
	  _HasLoaded=true; 
	  // set the players position to the data we loaded 
	}
	
	//***************************************************
	// Saving The Player...
	// **************************************************	
	if (GUI.Button(_Save,"Save")) {
             
	  GUI.Label(_SaveMSG,"Saving to: "+_FileLocation); 
	  myData._iUser.x=_Player.transform.position.x; 
	  myData._iUser.y=_Player.transform.position.y; 
	  myData._iUser.z=_Player.transform.position.z; 
	  myData._iUser.name=_PlayerName;    
          
	  // Time to creat our XML! 
	  _data = SerializeObject(myData); 
	  // This is the final resulting XML from the serialization process 
	  CreateXML(); 
	  _HasSaved=true; 
	  Debug.Log(_data); 
	}
	
	
   } 
    
   /* The following metods came from the referenced URL */ 
   string UTF8ByteArrayToString(byte[] characters) 
   {     
      UTF8Encoding encoding = new UTF8Encoding(); 
      string constructedString = encoding.GetString(characters); 
      return (constructedString); 
   } 
    
   byte[] StringToUTF8ByteArray(string pXmlString) 
   { 
      UTF8Encoding encoding = new UTF8Encoding(); 
      byte[] byteArray = encoding.GetBytes(pXmlString); 
      return byteArray; 
   } 
    
   // Here we serialize our UserData object of myData 
   string SerializeObject(object pObject) 
   { 
      string XmlizedString = null; 
      MemoryStream memoryStream = new MemoryStream(); 
      XmlSerializer xs = new XmlSerializer(typeof(UserData)); 
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
      xs.Serialize(xmlTextWriter, pObject); 
      memoryStream = (MemoryStream)xmlTextWriter.BaseStream; 
      XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); 
      return XmlizedString; 
   } 
    
   // Here we deserialize it back into its original form 
   object DeserializeObject(string pXmlizedString) 
   { 
      XmlSerializer xs = new XmlSerializer(typeof(UserData)); 
      MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); 
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
      return xs.Deserialize(memoryStream); 
   } 
    
   // Finally our save and load methods for the file itself 
   void CreateXML() 
   { 
      StreamWriter writer; 
      FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName); 
      if(!t.Exists) 
      { 
         writer = t.CreateText(); 
      } 
      else 
      { 
         t.Delete(); 
         writer = t.CreateText(); 
      } 
      writer.Write(_data); 
      writer.Close(); 
      Debug.Log("File written."); 
   } 
    
   void LoadXML() 
   { 
      StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName); 
      string _info = r.ReadToEnd(); 
      r.Close(); 
      _data=_info; 
      Debug.Log("File Read"); 
   } 
} 

// UserData is our custom class that holds our defined objects we want to store in XML format 
 public class UserData 
 { 
    // We have to define a default instance of the structure 
   public DemoData _iUser; 
    // Default constructor doesn't really do anything at the moment 
   public UserData() { } 
    
   // Anything we want to store in the XML file, we define it here 
   public struct DemoData 
   { 
      public float x; 
      public float y; 
      public float z; 
      public string name; 
   } 
}

Best
Narlix, aka Theodor :roll:

Thanks, I figured I had one two many (or so) “if” statements in there, I was just trying to be over careful as it were on making sure the OnGUI() loop didn’t cause a repetative read or repetative write. Working remote at the moment so I will take a gander at your changes in about 2 hours and apply them to my base code :slight_smile: Thanks for helping with this on clean up. Much appreciated.

Looks like after you clean up, there was some left over booleans :slight_smile: I have removed those and updated the final script. Here it is!

(note: I tried to upload the unitypackage and the rar or zip of it also and still got my good old errors of:

General Error
Tried to upload empty file )

So just the script file went.
http://www.gamedevonline.com/subsite/unity/gamesavingandloadingxml.rar

177349–6339–$_gamesaveload_141.cs (5.55 KB)

Thank you for this =)!