UNITY2D: Trouble running a CoRoutine from another script - c#

I have written a function on a script that activates when the player loses all his lives. That calls a CoRoutine in a script attached to my main character that makes a simple death animation and then moves to the game over screen. Debug.Log shows that the function calls, and when I use non-CoRoutine functions attached to the main character, those functions call to. However, the CoRoutine itself never calls, not even showing the log of it ever activating? Does anyone know what is up?
Code included below:
if (GameObject.Find("Heart 1") == null)
{
Debug.Log("Naw");
player.DeathAnimation(20);
Debug.Log("still not working");
}
public IEnumerator DeathAnimation(int i)
{
int k = i;
Debug.Log("Numerator works");
transform.Rotate(Vector3.forward * 9);
yield return new WaitForSeconds(.08f);
k--;
Debug.Log(k);
if (k <= 0)
{
SceneManager.LoadScene("Game Over");
yield break;
}
StartCoroutine(DeathAnimation(k));
}

There doesn’t seem to be a reason to make this a recursive coroutine. I’d suggest removing the recursion which might also solve the issue you’re having or at least make it simpler to identify.
if (GameObject.Find("Heart 1") == null)
{
Debug.Log("Hmm");
StartCoroutine( player.DeathAnimation(20) );
Debug.Log("maybe working");
}
…
public IEnumerator DeathAnimation(int i)
{
Debug.Log("Numerator works");
for( ; i>0; i-- ) {
transform.Rotate(Vector3.forward * 9);
yield return new WaitForSeconds(.08f);
Debug.Log(i);
}
SceneManager.LoadScene("Game Over");
}

Related

Making a for loop wait for user Input

I am programming a task in VR where the user hears an audio file with a certain amount of numbers and needs to press a key after that. If he did it right then he should press "r" and an audio file that is one number longer will be played. If he did it wrong he should press "w" and if it was his first wrong answer after a right answer an audio file will be played that has the same amount of numbers than before. If he did it wrong and already did it wrong right before that the amount of numbers is reduced by one.
In total there will be 10 audio files which is why I decided to use a for loop.
But now I don't know how I can make the for loop wait for the user input. I've also read that it is in general not god to use a for loop when you have to wait for a user input but I don't know what else I can do.
Here is my code so far:
IEnumerator playDSBtask()
{
bool secondWrong = false;
fillList();
for (int i = 0; i < 10; i = i + 1)
{
List<AudioSource> x = chooseList(score);
AudioSource myAudio = x[0];
float duration = myAudio.clip.length;
myAudio.GetComponent<AudioSource>();
myAudio.Play();
yield return new WaitForSeconds(duration + 5);
if (Input.GetKeyDown("r"))
{
score++;
secondWrong = false;
}
else if (Input.GetKeyDown("f") && secondWrong == false)
{
secondWrong = true;
}
else if (Input.GetKeyDown("f") && secondWrong == true)
{
score--;
}
x.Remove(myAudio);
}
}
But this won't work since the for loop will just continue if none of the if or else if statements are true and therefor executed.
Using a for loop in your case is totally fine since it is in a Coroutine where you yield inside of it so there will be no endless loop and therefore freeze of the main thread.
You could probably use WaitUntil something like
...
yield return new WaitForSeconds(duration + 5);
// Wait until any o the expected inputs is pressed
yield return WaitUntil(() => Input.GetKeyDown("r") || Input.GetKeyDown("f"));
// then check which one exactly
if (Input.GetKeyDown("r"))
{
...
alternatively this could also be done in a more generic way but also more error prone
...
yield return new WaitForSeconds(duration + 5);
// wait until something breaks out of the loop
while(true)
{
// check if any key was pressed
if(Input.anyKeyDown)
{
// handle r key
if (Input.GetKeyDown("r"))
{
score++;
secondWrong = false;
// important!
break;
}
else if (Input.GetKeyDown("f"))
{
if(!secondWrong)
{
secondWrong = true;
}
else
{
score--;
}
// Important!
break;
}
}
// if nothing called break wait a frame and check again
yield return null;
}
x.Remove(myAudio);
...
In general you might want to use KeyCode.F and KeyCode.R instead of strings.
From a UX perpective though you would either want to reduce the 5 seconds wait or/and show some kind of UI feedback for when user input is handled/awaited.

Make a UI Text pop for a few seconds on Unity

I'm pretty new to coding, been learning for the past year and I'm currently working on an assignment for school and i can't figure out this bit of code for the love of my life.
I have an item that when the player interacts with it, executes this:
void Update ()
{
if (isPlayerNear && Input.GetKeyDown(KeyCode.E) && Avatar.strenghtAttribute >= 2f)
{
levelUp.LevelUp();
Destroy(gameObject);
}
My level up function is basically this:
public void LevelUp()
{
playerLevelText.text = ("You have gained a level!");
strenghtAttribute++;
intellectAttribute++;
playerLevel++;
}
I'm trying to figure out how to make playerLevelText.Text appear on the screen but only appear for a few seconds and I can't figure out how to make that work. Would anyone be kind enough to give me a hand?
You could either set the text to be blank, or enable/disable the text object. I'd recommend using a coroutine for this.
void Update ()
{
if (isPlayerNear && Input.GetKeyDown(KeyCode.E) && Avatar.strenghtAttribute >= 2f)
{
levelUp.InitializeLevelUp());
Destroy(gameObject);
}
Since you are destroying the gameobject calling the coroutine, the coroutine will stop. A workaround is to call a normal function in your other script, which then calls the coroutine, so execution stays within the one script (there might be a cleaner way to do this).
public void InitializeLevelUp()
{
StartCoroutine(LevelUp());
}
public IEnumerator LevelUp()
{
playerLevelText.text = ("You have gained a level!");
strenghtAttribute++;
intellectAttribute++;
playerLevel++;
yield return new WaitForSeconds(2f);
playerLevelText.text = "";
//alternatively, set the text object inactive
}

Unity C# Yield WaitForSeconds within For Loop Only Works Once

I am trying to use two yields within a coroutine loop (because I need to iterate out arrays with pauses between each loop).
The first loop works correctly, with all the yields working for the right amount of time. By the second loop, the yield return new WaitForSeconds() begins counting down right away, not waiting for the yield and code before it to complete (it seems). By the time of the third loop, the timing is all off.
I tried using a while loop instead of a for but got the same result.
TLDR: I need to loop out my arrays with pauses between each one. How can I use more than one yield past the first loop through in a coroutine?
public IEnumerator doPathfinding()
{
for (int i = 0; i < waypoint.Length; i++)
{
// get first waypoint from array
var _waypoint = waypoint[i];
// get node on A* of cloest waypoint
closestPointOnNavmesh = AstarPath.active.GetNearest(_waypoint.transform.position, NNConstraint.Default).clampedPosition;
// Move towards destination
ai.destination = closestPointOnNavmesh;
// Wait until within X range of waypoint
yield return new WaitUntil(() => distanceReached == true);
// Agent is now at waypoint and automatically stops. Wait 5 seconds before looping to next waypoint.
yield return new WaitForSeconds(5f);
}
Debug.Log("Loop End");
}
public override void OnUpdate()
{
// Get current distance to the target. If distance is less than offset, then sent event.
currentDistance = Vector3.Distance(go.transform.position, closestPointOnNavmesh);
if(currentDistance <= arrivalOffset.Value)
{
distanceReached = true;
}
else
{
distanceReached = false;
}
}
The code inside the couroutine is fine, it works as intended.
Most probably, based on the issue you reported, you're calling more than once the coroutine at the same time.
Use a bool to check if the coroutine has already started or not, like this:
bool isDoPathFindingRunning = false;
IEnumerator = doPathFinding();
private void Awake() {
pathFinding = doPathfinding();
}
private void WhereeverYouStartCoroutine() {
if (!isDoPathFindingRunning)
StartCoroutine(pathFinding);
}
public IEnumerator doPathfinding() {
isDoPathFindingRunning = true;
// Do your stuff
isDoPathFindingRunning = false;
}

Delay within an if in C#

How can I create a delay after the fade in, so that the text stays on screen for a few seconds? I used an IEnumerator and a coroutine, but it does nothing. I also tried placing it right after the first else.
What happens at the moment is the text fades out before having the chance to fade in. The text appears momentarily in semi-clear and disappears. It's for a Unity project.
Also, Thread.Sleep won't do.
Here's the piece of code in question:
IEnumerator Pause ()
{
yield return new WaitForSecondsRealtime(5);
}
void OnTriggerStay2D(Collider2D interCollider)
{
if (Input.GetKeyDown(KeyCode.E))
{
displayInfo = true;
}
else
{
displayInfo = false;
}
}
void FadeText()
{
if (displayInfo == true)
{
text1.text = string1;
text1.color = Color.Lerp(text1.color, Color.white, fadeTime * Time.deltaTime);
}
else
{
StartCoroutine(Pause());
text1.color = Color.Lerp(text1.color, Color.clear, fadeTime * Time.deltaTime);
}
}
Your code should read:
void Update()
{
if (fadingOut)
{
// fade out with lerp here
}
}
IEnumerator Pause()
{
yield return new WaitForSecondsRealtime(5);
fadingOut = true;
}
void FadeText()
{
if (displayInfo == true)
{
text1.text = string1;
text1.color = Color.Lerp(text1.color, Color.white, fadeTime * Time.deltaTime);
}
else
{
StartCoroutine(Pause());
}
}
You have the right idea of using a coroutine, but didn't quite get the execution right. When you invoke a method on coroutine, it will be executed in parallel with the main thread. In your code, the Pause() method is running alongside the Color.Lerp. If you want the fading to wait until after the pause is complete, they must be on the same coroutine.
Edit:
As pointed out, this won't work if you're calling FadeText() on each frame. But this shows you how you can easily set a flag and wait until the pause time is complete before fading.
You just need to add the text fade to the coroutine.
IEnumerator Pause()
{
yield return new WaitForSecondsRealtime(5);
text1.color = Color.Lerp(text1.color, Color.clear, fadeTime * Time.deltaTime);
}
And just start the coroutine in your else statement. This way it will execute the wait for seconds, and then the fade whenever you call it.
Most easy way is to use LeanTween asset. It's free and have a lot of other usefull features that I use in EVERY project.
It's really awesome lib.
LeanTween.DelayedCall(1f,()=>{ /*any code that will be called after 1 second will pass*/ });
or
LeanTween.DelayedCall(1f, SomeMethodWithoutParams());

Input not being detected (yet input is specified)

I have the following function to manage the reloading process of my player's gun in my game. I call the function from an AmmoCheck function and it runs, however the if statement is never run because the input doesn't get detected. The line:
Debug.Log("Reload function check");
will print to the console, but nothing further.
I have made sure the Input is set up under Edit>Project Settings>Input, and it is set to the r button. (Proof - http://i.imgur.com/u2aVNpU.png )
The function:
void gunReload()
{
Debug.Log("Reload function check");
if (Input.GetButtonDown("Reload")) {
Debug.Log("Reloading");
if(changeWeapon.currentGun == changeWeapon.gunPistol) {
aReload.SetActive(false);
// Incrementing the aClipPistolCurrent value by 1 so the current clip "should" progress one along? idk
aClipPistolCurrent += 1;
}
if(changeWeapon.currentGun == changeWeapon.gunAssault) {
aReload.SetActive(false);
// Incrementing the aClipPistolCurrent value by 1 so the current clip "should" progress one along? idk
aClipAssaultCurrent += 1;
}
}
}
Function calling gunReload();
gunAmmoCheck() is being called inside of Update()
void gunAmmoCheck()
{
if(changeWeapon.currentGun == changeWeapon.gunPistol && aClipPistol[aClipPistolCurrent] > 0) {
gunFire ();
// Reducing the ammo of the current clip by 1.
// ClipPistol is being used (say array, why array
aClipPistol[aClipPistolCurrent] -= 1;
}
if(changeWeapon.currentGun == changeWeapon.gunAssault && aClipAssault[aClipAssaultCurrent] > 0) {
gunFire ();
// Reducing the ammo of the current clip by 1.
// ClipPistol is being used (say array, why array
aClipAssault[aClipAssaultCurrent] -= 1;
}
if(aClipPistol[aClipPistolCurrent] == 0)
{
Debug.Log ("Reload");
// Activating the reload notification on the interface
aReload.SetActive(true);
gunReload();
}
if(aClipAssault[aClipAssaultCurrent] == 0)
{
Debug.Log ("Reload");
// Activating the reload notification on the interface
aReload.SetActive(true);
noAmmo = true;
gunReload();
}
}
I'm at a loss here, since all of my other inputs work flawlessly. Any help would be greatly appreciated.
I just tested this and it worked. I am starting to think that the problem is where you are calling the function from. Not sure yet but
Lets find the problem slowly. Can you comment out those things in your function and have only
void gunReload ()
{
//Debug.Log ("Reload function check");
if (Input.GetButtonDown ("Reload")) {
Debug.Log ("Reloading");
}
}
in your code. Then call gunReload () from the Update and tell me if it works.
It would also be good to pose the function you are calling gunReload () from.

Categories