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 Potatobob · Apr 06, 2014 at 09:21 PM · waitforsecondsonlevelwasloaded

If scene just started wait 3 second before proceeding in the script.

Hi I would like some help with a short part of Scripting First of im sorry for the simple question but I am new and cant get this to work.

I am trying to put out in C# the code : If scene just started than wait 3 seconds before proceeding in the script.

I got everything else coming after but I really want this script to give the player some time to react at the begining of a scene.

I found this to begin with but cant find how to put it properly toguether

using UnityEngine; using System.Collections;

public class Test : MonoBehaviour {
void OnLevelWasLoaded(int Level1) { //error comes up in this line

     yield return new WaitForSeconds(3);
     
 }
 void OnTriggerEnter (Collider col)
 {
     if(col.gameObject.name == "Trigger1")
     {
             Application.LoadLevel("RPG");
     }
 }

}

finaly here is the error I get and I dont understand it :S

Assets/Test.cs(6,14): error CS1624: The body of Test.OnLevelWasLoaded(int)' cannot be an iterator block because void' is not an iterator interface type

Thx for the help :P

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
0
Best Answer

Answer by Kiloblargh · Apr 06, 2014 at 09:25 PM

You can't do that. Not all functions can have a yield in them, it's not really worth explaining why right now...

Instead, do this:

  Invoke ("LateStart", 3.0);

Then put all your delayed startup stuff in LateStart().

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 Kiloblargh · Apr 06, 2014 at 09:30 PM 0
Share

Also, if you think you're checking if Level 1 was loaded with OnLevelWasLoaded (int Level1) you're not. You're just na$$anonymous$$g an int variable "Level1", whose value will be the current level number, and you still have to then check if its value is 1 or something else.

avatar image Potatobob · Apr 06, 2014 at 10:06 PM 0
Share

Now I get this error : Assets/Test.cs(19,27): error CS1519: Unexpected symbol `LateStart' in class, struct, or interface member declaration

I worked on my code and this is what I have so far: using UnityEngine; using System.Collections;

public class Test : $$anonymous$$onoBehaviour { public Camera camera1; public Camera camera2; public bool Fighting = false;

 void Start ()
 {
     camera1 = GetComponent<Camera>();
     camera2 = GetComponent<Camera>();

     camera.enabled = true;
     camera2.enabled = false;
 }

 Invoke ("LateStart", 3.0);
 void OnTriggerEnter (Collider col)
 {
     if(col.gameObject.name == "Trigger1")
     {
         Fighting = true;
         camera1.enabled = !camera1.enabled;
         camera2.enabled = !camera2.enabled;
     }
 }
 void OnTriggerExit (Collider col)
 {
     if(col.gameObject.name == "Trigger1")
     {
         Fighting = false;
         camera1.enabled = !camera1.enabled;
         camera2.enabled = !camera2.enabled;
     }
 }

}

avatar image Kiloblargh · Apr 07, 2014 at 05:43 AM 0
Share

You can't Invoke a function that doesn't exist. You have to define a void LateStart () function somewhere inside Test. IF you do that, it will be called in 3 seconds.

You could also use Animation events for this purpose. The Animation can be set to play on Awake and you can have as many functions called after different delays as you like.

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

21 People are following this question.

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

Related Questions

Assign a clip to an AudioSource when it finishes playing. 1 Answer

UI shows text not in order 2 Answers

Loading content after loading scene 0 Answers

yield in C# doesn't work, not event the sample code? 1 Answer

Wait For Seconds to Load level 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