wait for seconds in c#
I know that i need to use IEnumerator MyMethod() and make a coroutine in order to make a script wait for seconds and then do something, but i can't do this inside my script =\
So, i have a OnCollision void and i wanted to use wait for seconds on it, like:
void OnCollisionEnter(Collision collision)
{
Wait 2 seconds
Do everything that i want
}
But i can't. I am having multiple problems, such as "this thing shouldn't be here", "you can't do this" and all. I have read the docs about this a lot of times but this just doesn't work.
Here is my script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class EAimSys : MonoBehaviour //This is my Enemy script. As it doesn't have any mouse or keyboard, i need to use the WFS code.
{
bool Click = false;
bool target = false;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public GameObject Flash;
public Animator anim;
void OnCollisionEnter(Collision collision)
{
{
if (collision.gameObject.tag == "Player") //Here is where i want to start.
//I wanted to use the wait for seconds code in here, so everything below this would need to wait x seconds before happening.
//Why? Because otherwise my enemy would kill me instantly and i don't have enough experience to do anything more complicated than this.
anim.SetTrigger("Shoot");
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
Flash.SetActive(true);
Debug.Log("Player Hit!");
}
}
void OnCollisionExit(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
Flash.SetActive(false);
}
}
}
Also sorry, but i couldn't find an answer for this so i needed to ask. After fixing this problem i will be almost done with the scripting part and i can finally release my game as the nice fps it is =')
Did you try my solution below. Besides the links to script, I provided a link to a demo Unity package. Please take a look at it.
It is a rough demo with a square (enemy) shooting (instantiating) a projectile at a sphere (player) every 2 seconds.
I did not add movement controls to the player but the sphere can be moved at run time in the scene view.
Answer by SrXoo · Nov 08, 2016 at 09:04 PM
Well, my answer might be considered a little "dirty" but it is a possible solution.
You could delegate all that logic to other method which updates in the Update(). Having a float variable with the seconds you want to wait and reduce it using deltaTime (1 second = 1f).
Answer by Razputin · Nov 08, 2016 at 09:04 PM
use coroutines
void OnCollisionEnter(Collision collision)
{
StartCoroutine(Fire());
}
Private Ienumerator Fire()
{
// do whatever you have to do to fire
yield return new WaitForSeconds(2);
StartCoroutine(Fire()); //if you put this here it will start the coroutine again over and over after 2 sec
}
Your answer
Follow this Question
Related Questions
StartCoroutine not listening to parameters 1 Answer
I want my script to wait 2 seconds before continue in a condition, in update, using C# 2 Answers
Hololens sharing example 1 Answer
How should I use the `FreeLookCam` script located in the standard assets folder 0 Answers
[C#]How to save a list of items to use in another script and it's not a player prefs type of list? 0 Answers