hi. Could anyone be interrested in helping me with this script?
i got a level where, when the FPS character falls of a platform, he is returned to starting point.
But what i want to happen, which i havent been able to do yet, is to reset the level to it original
state as well. Cause there are pickups, and platforms falling when colliding with them etc. So i want the
pickups, the fallen platforms, the score and time etc, to be reset to their original state when the characher fails.
what i got so far is:
RespawnScript connected to a boxCollider, which brings the characher to start when colliding with:
function OnTriggerEnter (fallTrigger : Collider) {
Debug.Log(fallTrigger.gameObject.name+" collision entered");
if (fallTrigger.GetComponent(FPSWalker) ) {
fallTrigger.GetComponent(FPSWalker).Respawn();
}
}
FPSWalker script which contain a respawn function:
var speed = 20.0;
var jumpSpeed = 25.0;
var gravity = 50.0;
var safeRespawn : Vector3;
var initialPosition;
var initialRotation;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Fire1")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
function Start () {
initialPosition = transform.position;
initialRotation = transform.rotation;
}
//function update() {
//if (InSafePosition() ) safeRespawn = transform.position;
//}
function Respawn() {
transform.position = initialPosition;
transform.rotation = initialRotation;
}
any help with this would be really appreciated now. Hope anyone can help me:)
Could you not just reload the level? would seem to be the quickest way of doing things 
Agreed; reloading the scene/level would likely be by far the easiest solution. (If that’s not suitable for some reason, perhaps you could explain what that reason is so that we can offer more appropriate suggestions.)
ah yes right, application.loadLevel right? Dont know how i missed that, thank you. But would i change that code with some of the code i already have?
Sorry, im not really a programmer, im not sure where i would place this code?
Thanks again, i really appreciate help on this.
On the event that you want to trigger the respawn 
i managed to reset the level with application.LoadLevel, however, my score does not reset.
If anyone could have a look at this, here are the score code:
static var text : String;
static var score:int;
function Update() {
guiText.text = text;
}
The score is connected with the timer, so here are the timer script:
var startTime;
var oldMinutes = 0;
function Awake() {
startTime = Time.time;
}
function Update () {
var Time = Time.time - startTime;
var minutes : int = Mathf.Floor(Time / 60);
var seconds : int = Time % 60;
var fraction : int = (Time * 100) % 100;
oldMinutes = minutes;
guiText.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
if (seconds != oldMinutes) {
scoreScript.score-=2;
}
}
hope anyone can help me 
Unless I’m missing something, the reason the score doesn’t reset is that the ‘score’ variable is static, and therefore its value will persist throughout the application’s run. If you want it to reset at the beginning of a level, you’ll need to reset it manually (e.g. by setting it to 0 in the Awake() or Start() function for a component that’s attached to a game object), or make it a non-static member variable (in which case you may have to access it in a different way than you are now).
thank you for youre answer, but sorry im not very skilled in scripting. I tried in the Awake function to add, scoreScript.score = 0;, and also score = 0;,
without any change. Do you think you could explain a bit more? Cant really find any solution on this any place
You could try removing the keyword static from before score, that should fix the problem - unless there is some reason you want it static. Then just adjust how you are accessing it. Though, just reseting it should also work fine, what code are you using?
thanks for answer, when i remove the static, i get the error: An instance of type scorescript is required to access non static member score. On all the places i try to access the score. So how would i be able to access it?
Im using a pickupScript, which is where i get the score when the player walks on the pickups:
function OnTriggerEnter(other:Collider){
Debug.Log(other.gameObject.name+" collision entered");
Destroy(gameObject);
scoreScript.score+=1000;
scoreScript.text = scoreScript.score.ToString();
}
im using a timeCountScript which also wants to access the scoreScript:
v
ar startTime;
var oldMinutes = 0;
function Awake() {
startTime = Time.time;
}
function Update () {
var Time = Time.time - startTime;
var minutes : int = Mathf.Floor(Time / 60);
var seconds : int = Time % 60;
var fraction : int = (Time * 100) % 100;
oldMinutes = minutes;
guiText.text = String.Format ("{0:00}:{1:00}:{2:00}", minutes, seconds, fraction);
if (seconds != oldMinutes) {
scoreScript.score-=2;
}
}
and of course the scoreScript it self:
static var text : String;
public var score:int;
function Update() {
guiText.text = text;
}
thanks a lot for all the help 
No problem - when the score is declared static, doesn’t this work ?
var startTime;
var oldMinutes = 0;
function Awake() {
scoreScript.score = 0;
startTime = Time.time;
}
Not got unity running at the moment, but I thought that might work?
sorry, but the result stayed the same. Is there anything that could affect this script? I couldnt imagine though if there isnt a script that has directly something to do with the scoreScript?? But as far as i can see, there isnt.
Resetting the score in a function that’s guaranteed to be run when the level is started should work. If it doesn’t seem to be working, use the debugger or add some debug output to determine:
- If the function is in fact executing.
- If the line of code that resets the score is executing.
- What the value of ‘score’ is before this code executes (if it executes).
- What the value of ‘score’ is after this code executes (if it executes).
- What the value of ‘score’ is wherever you query it and display it.
ok, so i added Debug.Log, after the code that reloads the level, and it did not turn out 0, but not the score
that showed in the score area either, it showed a different number in the log each time (less than what displayed in the score area). The timer and score are connected, so the more time that has pass, the less will the pickups be worth. But the timer resets successfully when the level is reloaded. Still, could it have something to do with that?
Code where i added Debug.Log:
private var customButton : GUIStyle;
private var triggerEnd : boolean;
function OnTriggerEnter (other : Collider) {
triggerEnd = true;
}
function OnGUI () {
if (triggerEnd == true) {
if (Time.timeScale == 1.0) {
Time.timeScale = 0.23;
}
GUI.Box (Rect (450, 150, 350, 350),"good job");
if (GUI.Button (Rect (590, 200, 80, 20), "Play Again")) {
Time.timeScale = 1.0;
Application.LoadLevel (1);
Debug.Log(scoreScript.score+ "works");
}
if (GUI.Button (Rect (590, 250, 80, 20), "Quit")) {
Application.LoadLevel (0);
}
}
}
Put a debug log in your Awake() function, it should always fire when your level reloads.
var startTime;
var oldMinutes = 0;
function Awake() {
Debug.Log ( "I am awake!" );
scoreScript.score = 0;
startTime = Time.time;
}
Look for when you see I am awake! in the console log, if it is in the right place, it should output everytime the level is re-loaded
thanks. So, it does trace out every time the level loads. Any more thoughts on this?
I really appreciate the help im getting here!
could it have something to do with, removing the static var and add GetComponent to access the script? I tried but got a few different errors, my old ones disappeared though.