- Home /
 
If Dead,Checkpoint and reset
Checkpoint script:
function OnTriggerEnter(col : Collider) {
if(col.gameObject.tag == "Player")
col.GetComponent(MyEnd).checkpoint = transform;
}
MyEnd script:
var checkpoint : Transform;
function MyRightEnd() {
transform.position = checkpoint.position;
//All then changes not save
}
How load checkpoint,but then changes not save?
I am not sure I follow. Please elaborate what you are trying to achieve.
Answer by Rydrako · Aug 06, 2011 at 02:03 PM
Well first of all you instead of using a transform variable, for your MyEnd script, just make 3 static float variable for x position, y position, and z position. So in in your checkpoint script say:
 MyEnd.xPos = transform.position.x;
 MyEnd.yPos = transform.position.y;
 MyEnd.zPos = transform.position.z;
 
               instead of:
 col.GetComponent(MyEnd).checkpoint = transform;
 
               and then in a new script say:
 // Import the System.IO Classes
 import System;
 import System.IO;
 // Set the file path variables
 static var filePath2 : String = "C:/ProgramData"; //a hidden folder in computers.
 static var fileName2 : String = "GameData";
 static function Save ()
 {
    // create vars
    var saveXPos : String = "" + MyEnd.xPos;
    var saveYPos: String = "" + MyEnd.yPos;
    var saveZPos : String = "" + MyEnd.zPos;
    // Open the file
    var saveStream : StreamWriter = new StreamWriter (filePath2 + "/" + fileName2 );
   
    // Write the save data
    saveStream.WriteLine(saveXPos);
    saveStream.WriteLine(saveYPos);
    saveStream.WriteLine(saveZPos);
 
   
   // Close the file
   saveStream.Close ();
   
   // Give confirmation that the file has been saved
   print ( "Data Saved at: " + filePath2 + "/" + fileName2 );
 
               } so now in any script you can just say for example:
 SaveScript.Save();
 
               to save your checkpoint progress and to load it make another script:
 import System;
 import System.IO;
 static var filePath : String = "C:/ProgramData";
 static var fileName : String = "GameData";
 function Load ()
 {
      //make vars
      saveXPos : String = "";
      saveYPos : String = "";
      saveZPos : String = "";
 
      //Opeb the file
      var saveStream : StreamReader = new StreamReader ( filePath + "/" + fileName );
      //read the save data
      saveXPos = saveStream.ReadLine();
      saveYPos = saveStream.ReadLine();
      saveZPos = saveStream.ReadLine();
      //turn the string into numbers
      var intX = float.Parse(saveXPos);
      var intY = float.Parse(saveYPos);
      var intZ = float.Parse(saveZPos);
      //now we have variables to use
      MyEnd.xPos = intX;
      MyEnd.yPos = intY;
      MyEnd.zPos = intZ;
 }
 
               ...phew, hope that helps! :)
JEEEESE you are a legend mate ive been trying to figure this out for about a week now
I just looked up how to save player progress in unity for my own game. :)
Your answer