- Home /
The question is answered, right answer was accepted
LoadLevel Health Question.
OK so i use those two script you can see below for my player's hearts. How is it possible to make the hearts be minus 1 when i press the Respawn button and the level is loaded? Also, i noticed that some objects that were destroyed at first, were loaded together with the level and i dont really want that. Thanks!
FallDamage.js
var hearts : int = 3;
function OnGUI(){
GUI.Box(new Rect(20,20,100,40), "Hearts : " + hearts);
if(hearts < 0){
hearts = 0;
}
}
TriggerFloor.js
var showGUI : boolean = false;
function OnTriggerEnter(other:Collider){
var otherScript : FallDamage = GameObject.Find("Player").GetComponent(FallDamage);
otherScript.hearts -= 1;
if(other.gameObject.name == "Player"){
showGUI = true;
}
}
function OnGUI(){
if(showGUI){
if(GUI.Button(new Rect(20,70,100,50), "Respawn")){
Application.LoadLevel(Application.loadedLevel);
}
}
}
Answer by fafase · Apr 20, 2013 at 05:16 PM
Maybe you want to avoid to reload the whole level since it seems you only want to respawn your player.
So you would have:
function OnGUI(){
if(showGUI){
if(GUI.Button(new Rect(20,70,100,50), "Respawn")){
transform.position = originalPosition;
hearts = -1;
}
}
}
Now if you really want to reload you could have a Start
function Start(){
hearts = -1;
}
then you can use DontDestroyOnLoad to save some object from destruction:
http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html
Unfortunately i get the error $$anonymous$$ identifier "originalPosition". :(
What's more, is this going to work is there is no Application.LoadLevel? :S
Ok i added this at the start of the script: var original = Transform; . But when i press the button nothing happens :/
Because you need to assign something to that variable.
function Start(){
original.position = transform.position;
}
Ok it seems that the moment i write the above lines: function Start(){ etc. i get a "Cannot convert 'UnityEngive.Vector3' to 'UnityEngine.Transform' error.
Follow this Question
Related Questions
Basketball Score counter 1 Answer
How do I use a trigger to change the GUIText 2 Answers
Health bar only updates on character death 1 Answer
4.6 Trigger.js Wrong command is running. Help? 0 Answers
Can't move GUI 1 Answer