Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MatthewSmoothy · Jul 04, 2012 at 12:01 PM · c#yieldwaitforseconds

WaitForSeconds/Yield problem

Hello, Can somebody tell me what im doing wrong with this script, im clueless and can not get this WaitForSeconds to work. I want it to wait 3 seconds before it goes back to my main menu, the error at the moment says: The name 'yield' does not exist in the current context. this is the script

 using UnityEngine;
 using System.Collections;
 
 public class endRace : MonoBehaviour {
  
  private startRace race;
  public GameObject GameOver;
  public float WaitForSeconds;
  
  // Use this for initialization
  void Start () {
  
  race = FindObjectOfType(typeof(startRace)) as startRace;
  
  }
  
  // Update is called once per frame
  void Update () {
  
  }
  
  void OnTriggerEnter(Collider raceTrig)
  
  {
  
  //Stops timer and ends race
  if(raceTrig.gameObject.tag == "Player")
  {
  race.raceStarted = false;
  GameObject.Instantiate(GameOver);
  yield.WaitForSeconds(3); //wait 3 seconds
  Application.LoadLevel("mainmenu"); //then return to main menu
  } 
  
  }
 
 }
Comment
Add comment
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

1 Reply

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

Answer by nventimiglia · Jul 04, 2012 at 12:39 PM

Please format your code. Your lucky I could read it.

Use a Coroutine. http://docs.unity3d.com/Documentation/ScriptReference/Coroutine.html

 void OnTriggerEnter(Collider raceTrig){
     StartCoroutine(StopRace());
 }
 
 IEnumerator StopRace(){
     race.hasStarted = false;
     GameObject.Instantiate(GameOver);
     yield return new WaitForSeconds(3);
     Application.LoadLevel("mainMenu");
 }
Comment
Add comment · Show 3 · 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 Wolfram · Jul 04, 2012 at 01:07 PM 0
Share

Correct. In C#, as opposed to US/JS, you cannot simply put yield statements into normal functions. You need to do it the way @nventimiglia describes (also the way he deals with "WaitForSeconds").

avatar image Fattie · Jul 04, 2012 at 01:23 PM 0
Share

It is really incredible to be trying to $$anonymous$$ch a beginner programmer how to use these (do not even mention the US/c# duality).

it is, really, impossible someone could really understand WHAT IS GOING ON inside such system-level program$$anonymous$$g when they are still just getting going with "understanding the syntax of the simplest commands".

avatar image Wolfram · Jul 04, 2012 at 01:32 PM 0
Share

It's not really about system-level program$$anonymous$$g, both are perfectly valid methods for specific tasks, and except for the more complicated syntax in C# (which is irrelevant since 99% of all Unity beginners use JS), they are both comparable. Except that Invoke() cannot be controlled on a frame-by-frame basis, and the time given is therefore only a rough estimate, and cannot be relied upon 100% (especially when using multiple calls or short intervals). Also, each call to Invoke will create an instance of a Coroutine internally, while you can put many yields into a single Coroutine.

If you need to do one specific thing at one specific point in time, use Invoke. If you want to create a sequence of logically connected tasks, use Coroutines.

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

9 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

C# WaitForSeconds doesn't seem to work ?? 2 Answers

Distribute terrain in zones 3 Answers

Waiting twice inside coroutine (C#) 2 Answers

Another question about yield, WaitForSeconds in 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