I am trying to insert a 5 second delay, separating two Get.Components at the end of the below code.
Tried using the iEnumerator function, but not getting it to work.
Any ideas?
using UnityEngine;
using System.Collections;
public class OnTriggerStayTimerFrogDeactivate : MonoBehaviour
{
public GameObject projectilePrefab;
float t = 0;
private bool canInstantiateProjectile = true;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Fly")
t = 0;
}
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Fly")
t += Time.deltaTime;
if (t > 0)
{
if (canInstantiateProjectile)
{
GetComponent<AI_FindObjectsWithtag_Transform_Fly>().enabled = false;
}
GetComponent<Animator>().CrossFade("Frog_Static", 0f);
// I'D LIKE TO INSERT A 5 SEC DELAY HERE
GetComponent<AI_FindObjectsWithtag_Transform_Fly>().enabled = true;
GetComponent<Animator>().CrossFade("Frog_Hungry", 0f);
}
}
}
No, you do not need to create a separate script. You can use a coroutine as well but using Invoke is simpler and easier.
Check the page I linked for an example on how to call a method with a delay using āInvokeā.
Sorry, Iām a little confused- that example was javascript and Iām working in C#.
Would you have any suggestions how to accomplish this in C#?
Thanks.
there is a language toggle top right on pretty much all the scripting reference pages.
Youāre going to run into issues any which way you do the delay since you are calling it from āOnTriggerStayā which is called every frame, so you will start a new coroutine, or invoke a new function every frame, which will all then run their course and not end up doing what you want.
Perhaps you could explain what you are trying to achieve (and I donāt expect an answer of āIām trying to put in a delayāā¦ :P)
For both the Invoke and the coroutine approaches: place the code after your comment in a new method.
If you want to use Invoke: simply call the method using Invoke(āYourNewMethodā, 2);
If you want to use coroutines:
void OnTriggerStay(Collider other)
{
// Beginning of your method here
// ...
GetComponent<Animator>().CrossFade("Frog_Static", 0f);
// I'D LIKE TO INSERT A 5 SEC DELAY HERE
StartCoroutine(WaitAndAnimate(5.0F));
}
IEnumerator WaitAndAnimate(float waitTime) {
yield return new WaitForSeconds(waitTime);
GetComponent<AI_FindObjectsWithtag_Transform_Fly>().enabled = true;
GetComponent<Animator>().CrossFade("Frog_Hungry", 0f);
}
And as LeftyRighty said, make sure you add a check so you donāt start this delay timer every frame.
I donāt know, is it? Youāre the one who can see your console, not us. You saying itās āthrowing errorsā, but donāt tell us what those errors are.
Thanks, StarManta.
I re-wrote a couple of lines and added in the OnTriggerStay- not sure if really needed.
Regardless, Iām still getting a parsing error and:
OnTriggerStayTimerFrogDeactivate2.cs(48,27): error CS1525: Unexpected symbol (', expecting )ā, ,', ;ā, [', or =ā
My current code:
using UnityEngine;
using System.Collections;
public class OnTriggerStayTimerFrogDeactivate2 : MonoBehaviour
{
public GameObject projectilePrefab;
float t = 0;
private bool canInstantiateProjectile = true;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Fly")
t = 0;
}
void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Fly")
t += Time.deltaTime;
if (t > 0)
{
if (canInstantiateProjectile)
{
GetComponent<AI_FindObjectsWithtag_Transform_Fly>().enabled = false;
GetComponent<Animator>().CrossFade("Frog_Static", 0f);
}
// THIS IS WHERE I WANT TO HAVE 5 SEC DELAY
StartCoroutine(WaitAndAnimate(5.0F));
IEnumerator WaitAndAnimate(float waitTime) {
yield return new WaitForSeconds(waitTime);
GetComponent<Animator>().CrossFade("Frog_Hungry", 0f);
GetComponent<AI_FindObjectsWithtag_Transform_Fly>().enabled = true;
}}}}