Hi there!
I am having a really bizarre issue where the following code loop exits as soon as it touches the yeild return new wait statement. I am calling this in a coroutine and it works perfectly when called on the player object, but not when called on an enemy object. I am attaching the GameController.cs where the player size increases incrementally, along with the PlayerAttackCollision.cs where the enemy size increases. The latter is experiencing this issue. It runs the loop below once in that instance, despite the index being 0 and the scaleIncreaseDurationMilSecs value being 90. Any advice or ideas are greatly appreciated!
IEnumerator updateScale(){
float incrementalSizeIncrease = calculatedMassChange / scaleIncreaseDurationMilSecs;
for(int i = 0; i < scaleIncreaseDurationMilSecs; i++){
enemy.transform.localScale += new Vector3(incrementalSizeIncrease, incrementalSizeIncrease, incrementalSizeIncrease);
yield return new WaitForSeconds(0.001f); //Wait for a milisecond before next iteration.
}
}
2342060–158392–PlayerAttackCollision.cs (2.07 KB)
2342060–158393–GameController.cs (6.11 KB)
You can yield return null to wait one frame, and use Time.deltaTime to get the amount of time in between frames.
WaitForSeconds does not give you the kind of granularity on time that you’re trying to use it for here; it’s useful for things that are longer than one frame, not shorter. There’s very little reason you’d ever need something shorter than one frame, except for physics (which is why FixedUpdate exists, and there is a yield instruction WaitForFixedUpdate).
However, for anything that is visual feedback, you never want to waste CPU cycles doing it more than once a frame.
I just described a technique in another thread that would be useful here: How do i fade a object in/out over time? - Unity Engine - Unity Discussions
Hi StarManta!
Thank you for taking the time to help me. I implemented your suggestion more or less exactly as you said in your example, and I’ve also tweaked it a bunch, but the loop always cancels on yeild return null. If there is any yeild statement in my loops, the loop immediately ends on it.
Paste your current code, let’s see if there’s any issue with the tweaks.
Is it possible that the object on which the coroutine is running is either being destroyed or disabled after it starts?
Hmmm, well it is definitely not being destroyed. I am not sure what disabled would mean in this context, but I am firing at an enemy who is not being destroyed or disappearing. It is also firing back at me. However, each hit only does the partial scale increase on it, because it only enters the loop once, and then exits on the yield statement. The weirdest aspect is that I developed this enemy character by copying the player character. The code should be essentially the same too. If the enemy hits me, my character expands in size. If I hit him, the same code should act the same, but when hitting the enemy, it jumps out of the loop. The same loop works fine when my player character is being hit.
Put a Debug.Log after the loop to see if the loop is being short-circuited somehow, or if it’s simply not executing the rest of the coroutine.
Good idea. I just did that. The debug line after the loop never gets hit. Its as if the yeild return statement is acting identically to a regular method return, immediately ending this coroutine.
Can you paste your code as it is now, including where it’s being called from?
Sure!
So I’ve uploaded PlayerAttackCollision.cs ---- This file is attached to the “Pulse” prefab object which is the player’s attack. This object will hit the Enemy which is a “CorruptedFurion” (tag) and will deal the “damage” to that collider. The damage here is ultimately the scale of this particular enemy. if it exceeds the maximum or minimum scale, it is destroyed.
The scale increase from each hit is intended to be dealt in a loop so that the enemy doesn’t instantly double in size, but seems to expand quickly from current size to final size.
Meanwhile, I also attached GameController.cs. This one I have yet to change from the original code above, but it has always worked for me. In it, you will see the IEnumerator updateScale. This one uses my old yeild new WaitForSeconds, and makes my player expand very smoothly from one size to another after being hit by the enemy projectiles. Now, the enemy projectiles have EnemyAttackCollision.cs attached to them, and that class calls the GameController.cs class to update the player size.
Basically the only difference in the working code and unworking code was that the player object was being updates by passing the collider to another class, where the non-working code is directly modifying the collider.gameObject without passing it to another class.
2342321–158409–PlayerAttackCollision.cs (2.53 KB)
2342321–158410–GameController.cs (6.1 KB)
2342321–158411–EnemyAttackCollision.cs (878 Bytes)
Well, this wouldn’t cause your primary issue, but this:
float normalizedPercentage = Time.deltaTime / localTime;
should be
float normalizedPercentage = localTime / scaleIncreaseDirectionSecs;
I think your actual problem is this:
I assume that “Pulse” is a bullet object that is destroyed after the hit? Try running the coroutine on the enemy object instead. That’s the key difference - your working code is being run from your GameController, which I assume is persistent.
If these effects are the same, you may even want to add a Transform parameter to the GameController. Have the bullet that hits the Player call this function with the player’s transform, while the thing that hits the enemy calls it with the enemy’s transform.
Your comments were wonderful! I ended up moving the code for editing the size of the enemies into the game controller with the player code. Now it works perfectly. I have utterly no clue why simply moving it there fixes it though. But it’s working now/
Because a coroutine is being run on the script that contains it, and it stops running when the object is destroyed. Your coroutine was on the Pulse object; when that object got destroyed, the coroutine running on it is no longer running.
ahhhhhhh, well that would explain it
I feel dumb. Thanks!