- Home /
Static function isn't working at all.
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.
I know for a certain that all variables in a static function also have to be static... so unless 'lastDiedLevelIndex' isn't static... I can't see a problem with your call of the function. There must be something else going wrong...
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, $$anonymous$$y.
Try to set the function Public.
public static function(or void on C#) Died(){...}
There is no error?
Ok, 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?
@Sydan Nope, all the variables in that script are declared as static
@Sister$$anonymous$$y 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.
Answer by Setzer22 · Sep 08, 2011 at 07:07 PM
Well, after a time searching I've found out that the yield line in the static function was the problem. As I found in one post on the unity forums:
They are object bound and a static function doesn't have any object So that was the problem. I will find some way to fix it. Thank you everyone for your answers.Yield can not work in static functions.
Living is learning! I'm glad to know that - it may avoid future headaches.
To solve your problem, I suggest to move part of the code to Update and use a static boolean variable to enable level loading:
static var reloadTime: float = 0;
static function Died() { reloadTime = 3; // reload level in 3 seconds ... }
function Update(){ if (reloadTime > 0){ reloadTime -= Time.deltaTime; if (reloadTime
Your answer
Follow this Question
Related Questions
Trying to create a static class for WWW-based functions and such 0 Answers
trying to delay a loop using yield - not working 1 Answer
Static IEnumerator!? 1 Answer
Static and Coroutines help 2 Answers