- Home /
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
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().
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.
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;
}
}
}
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
Follow this Question
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