Hello everyone,
I’m using this code in my game to make my character die and restart the level. First I’ve got an object (with the DontDestroyOnLoad function so it won’t be erased everytime) with a script named CheckPoints attached. And i’ve got another script named counters attached to the main character.
So this is the code in the Counters script:
function Die (){
var deadMonkey = Instantiate(dead_model, transform.position, transform.rotation);
CheckPoints.lastDiedLevelIndex = Application.loadedLevel;
CheckPoints.savedlifes -= 1;
CheckPoints.Died();
Destroy(gameObject);}
And this is the code in the checkpoints Script (all of the variables and functions from the checkpoints script that I am refferencing in the other script above are static and are from the CheckPoints script):
static function Died() {
Debug.Log("TRUE");
yield WaitForSeconds(3);
Application.LoadLevel(lastDiedLevelIndex);}
So the problem is when I execute the Died function in the first function. It simply doesn’t work (I don’t see the level loading nor the debug.log), the other things in the Die() function just work fine.
I am missing something very obvious? Or it’s just I am doing something wrong?
Does anybody know?
Thank you.
wait... you want to restart the level or load a different level each time? I don't see anything wrong with your script but I'm a bit confused that you would load a completely different scene each time you die...? However, I don't think that would explain why "TRUE" doesn't get printed... ... and you don't get any error-message at all or anything? Are you sure Die(); gets called as it should? This is really strange... Greetz, Ky.
– SisterKyTry to set the function Public. public static function(or void on C#) Died(){...} There is no error?
– anon25366562Ok, i think i've found where the problem is. If I remove the "yield WaitForSeconds(3)" line it works fine, I can always make manually a timer variable, but why isn't this working? is there any issue with yield and static functions?
– Setzer22@Sydan Nope, all the variables in that script are declared as static @SisterKy Well, what I want to do is to restart the level, I don't know any other way than loading the current level again. And yes, I am preety sure I am calling it well. I call another static functions from that script exactly like this in other parts of the code. Is that yield thing that seems to be causing some trouble. @Borgo Setting the function on Public didn't work neither and nope, it doesn't show up any error.
– Setzer22Ah, sorry, I misinterpreted your code.
– SisterKy