Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Setzer22 · Sep 08, 2011 at 02:14 PM · staticyield

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sydan · Sep 08, 2011 at 02:28 PM 0
Share

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...

avatar image SisterKy · Sep 08, 2011 at 04:28 PM 0
Share

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.

avatar image Borgo · Sep 08, 2011 at 04:49 PM 0
Share

Try to set the function Public.
public static function(or void on C#) Died(){...}

There is no error?

avatar image Setzer22 · Sep 08, 2011 at 06:50 PM 0
Share

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?

avatar image Setzer22 · Sep 08, 2011 at 06:54 PM 0
Share

@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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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:

Yield can not work in static functions.

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.
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Sep 08, 2011 at 09:14 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

Calling non-static from static function C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges