would someone please tell me how to use WaitForSeconds in C# ?
I tried to use it in different ways but nothing happens , no errors tho
one of the example is
using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour
{
public Transform enemy;
void Start()
{
Wait();
Instantiate(enemy);
}
private IEnumerator Wait()
{
yield return new WaitForSeconds(10);
}
}
another example
using UnityEngine;
using System.Collections;
public class EnemySpawn : MonoBehaviour
{
public Transform enemy;
void Start()
{
new WaitForSecods(10);
Instantiate(enemy);
}
}
neither works
would someone please tell me what I’m doing wrong ?
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
IEnumerator Start () {
Debug.Log("start waiting");
yield return new WaitForSeconds(2.0F);
Debug.Log("finished waiting");
}
}
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
public Transform cube;
IEnumerator Start () {
yield return new WaitForSeconds(2.0F);
Transform tmp = (Transform) Instantiate(cube, new Vector3(0.0F, 0.0F, 0.0F), Quaternion.identity);
yield return new WaitForSeconds(2.0F);
Destroy(tmp.gameObject);
}
}
thank you very much for your answer
it finally worked
do you have time please to explain the difference of your code from the first code I mentioned ?
does it have to be in start function ? or it can be anything ?
was my mistake by putting the word private on it ?
isn’t IEnumerator some sort of functions ? that I can call it inside other functions like start and update ?
sory for all those questions but I’m starting in C# and only know the basic stuff not the advanced ones
I appreciate your help
Thank u NPSF3000 for the links
I already read them couple of times but I didn’t understand it quiet well as I’m not very good on C# yet
I know it is very easy on javascript but I spent alot of time to learn C# so I prefer to use C# , I even tried to make a javascipt just for yield statements cause of its simplicity there but then I didn’t know how I’m supposed to call it from a C# or I didn’t give it a serious try
Stick with C# is my advice also. Besides some Unity specific stuff which you can find in the docs, you’ll be better off.
The interface containing the yield should be of type Enumerator :
void Start () {
StartCoroutine("Wait");
}
private IEnumerator Wait() {
yield return new WaitForSeconds(2.0F);
Transform tmp = (Transform) Instantiate(cube, new Vector3(0.0F, 0.0F, 0.0F), Quaternion.identity);
yield return new WaitForSeconds(2.0F);
Destroy(tmp.gameObject);
}
Okey thank you all I think I got it now and I will stick to C# as well
btw , is there a way to call a function on JS from C# ?
in C# I just need to make a public static function in any script to be able to call that function from another script and I do that by just typing the name of the script followed by comma and the function name
but I don’t know how I supposed to call a JS function from another C# script , is that possible ?
To interact with JS scripts from C# ones you need to move the JS into “Standard Assets” folder because it is one of the folders that are check first during compilation (read here).
well I’m sorry I think I still didn’t get it right
it seems that all code inside update() is executing parralel to the WaitForSeconds
what I want to do is that , in the start after the instantiation of the prefab I need to wait for 5 seconds then I change the animation and the rotation of it then I wanna wait for another 5 seconds before the code in the update start to work
so I’v put all those in the start() function and called the couroutine from there
then in the update() function I’v put the code to translate the prefab and make it auto move
but that doesn’t work this way , as it starts to move at the same moment it gets instantiated
anyway here is the code if someone might like to help
using UnityEngine;
using System.Collections;
using System.Globalization;
public class EnemyAnimation : MonoBehaviour
{
void Start()
{
StartCoroutine("Delay");
}
IEnumerator Delay()
{
animation.Play("idle");
animation["idle"].wrapMode = WrapMode.Loop;
yield return new WaitForSeconds(5f);
Quaternion q = UnityEngine.Random.rotation;
transform.rotation = new Quaternion(transform.rotation.x,
q.y, transform.rotation.z, transform.rotation.w);
}
void Update()
{
animation.Play("walk");
animation.wrapMode = WrapMode.Loop;
transform.Translate(Vector3.forward * Time.deltaTime);
}
}
with this code it starts the walk animation and move around at the moment it gets instantiated
the instantiation is in another script attached to the prefab
if I comment the update function it works as I need but ofc it doesn’t move
so anyone would tell me what is wrong in my code please ?
or another way to pause the execution better than the WaitForSeconds()?
okey ty but what next ? how to call it from C# ? would u give a snippet for the calling code ?
cause when I did that I still don’t get it in VS intellisense
yeah I finally decided to do what you said appels
a little dissapointed tho that there is nothing easier on Unity engine to achieve that
making it works only depends on the C# not unity specific function or mrethod that ease our work like OnTriggerEnter and alot of other cool methods
thank you sidev I’m gonna look up for it , hope it is free
my concern is if using couroutine can suspend the Update() function from running till the couroutine finishes , that doesn’t happen with me , may be cause I’m doing something wrong, but so far the update() function run its code even if the couroutine didn’t finsh
Yes,
In order to do that you need to use the concept of CoUpdate() on top of Update(). Here is some code I make for punleto before:
using UnityEngine;
using System.Collections;
public class Punleto : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine(CoUpdate()); //This is REALLY IMPORTANT
}
// Update is called once per frame
void Update () {
}
//This is the best way to put non performance sensitive code. ALWAYS create a CoUpdate() !!! believe me.
//Note: Coroutine Manager will help you stop, pause, resume or even reset this coroutine at will! Go for it.
IEnumerator CoUpdate(){ //ALWAYS have a CoUpdate, you ll thank me later...
while (true){ //Not necessary if you are using the "loop" mode in Coroutine Manager
/* do some of your code there */
//NOTE: this code will loop forever until you stop it based on a given condition.
print("doing some stuff...");
yield return StartCoroutine(WaitForXSeconds(3.0f));
print("now we are done waiting. Let s move on");
yield return 0; //Do not erase this without caution!
}
}
IEnumerator WaitForXSeconds(float seconds){
print("starting");
yield return new WaitForSeconds(seconds);
print("done!");
}
}
thank you again sidev , I’m gonna try that
I’ve put all the code inside the Update() function and used a variabe to store the time and I check it for a specific value during the game , when the" if " is true I do my code then reset the variable
I know this may be not efficient and may result in problems later but I hope not
if I find it gonna give me alot of trouples I’m gonna use the last code you provided , thank you again