49

This doesn't seem to have anything to do with how you set up the coroutine. What is probably happening is that the player gets a high enough acceleration while falling to glitch past the ground and the fallout collider. When you move the position during the respawn, it will still be falling at the same rate. In your Respawn method, sometime after your wait ...


14

The reason is the keyword yield which has a specific meaning in C#. On encountering the words yield return a function in C# returns, as one would expect. Using yield to define an iterator removes the need for an explicit extra class [...] When a yield return statement is reached in the iterator method, expression is returned, and the current ...


8

You can use Time.realtimeSinceStartup as reference for the time passed: IEnumerator Test() { float startTime = Time.realtimeSinceStartup; foreach( Raycast rc in RaycastArray) { if( Time.realtimeSinceStartup - startTime < 0.01f ) { //Process stuff for 10 miliseconds //PROCESS A RAYCAST } else { ...


8

When the fire button is lifted, the second if statement is entered, and StopAllCoroutines is run. This means that the Coroutine that the while loop is running in is ended, thus there is no more infinite loop. The coroutine is like a container for the code to execute in. I can recommend the Unity Manual and the Unity Scripting API to get a better grasp on ...


6

A bit of background on this: StartCoroutine is a method provided by the MonoBehaviour base class. MonoBehaviours are designed to be attached as components to GameObjects - Unity will warn you if you try to construct one with new, instead you should create them via gameObject.AddComponent<MyMonoBehaviourType>() The GameObject is how the engine keeps ...


3

This is my version of Leo's answer. Pretty handy tool for splitting up the workload of certain loops to help with FPS. Bit hard to predict how long each loop will take though, but probably something I'll work on further. IEnumerator ForOverTime (int fpsLimit, float timeLimit) { float processStartTime = Time.realtimeSinceStartup; float frameStartTime ...


3

The obvious solution would of course be to not yield after every queue entry but only after every n entries, where n is a number you need to figure out through experimentation. But that will of course lead to irregularities if some actions take much longer to invoke than others. An ideal solution would not process n queue entries but rather process queue ...


3

It's not true that you can't make a method in a base class working as a coroutine. If I understood you correctly, the AssessGeneration method is inside your base class, Generation, and you want it to be able to yield. In order to do so, make its return type an IEnumerator instead of void: public IEnumerator AssessGeneration(Func<NeuralNet, Task<...


3

Calling yield break breaks the coroutine and the while loop in it won't start over again. The while loop goes through only once after waiting for 0.3 seconds. Use the yield break only when you want to break out from the coroutine. If you want the while loop to go through multiple times, try yield return null; which will just make the coroutine to wait for ...


3

A MonoBehaviour's Update() method runs once, to completion, before each game frame is rendered. So if you change things in the middle of your Update method, you won't see that change until the Update method finishes (by reaching the closing brace at the end or a return statement), giving the engine a chance to continue with the rest of its work for the frame,...


3

It has nothing to do with it being a coroutine. It's an infinite loop, that can't exit. You need to return control to the code that's calling the coroutine. Your idea is good, it's just missing that component. If you're paused, just return without doing anything: while (timeleft > 0) { if (pause) { yield return null; continue; ...


3

You can do that if you want, but... First, 10000 update calls are more expensive than one update call calling 10000 functions. This is due to the Magic Method indexing and querying logic Unity does for you. Second, adding, removing, and fetching components has its own overhead. And it's not a cheap as you think. It isn't heavy, but you absolutely don't ...


3

If each call to Update calls a coroutine, then you are creating a new coroutine each update and each coroutine will run in parallel with those created before. Your object will rotate faster and faster, while your framerate will get lower and lower due to the huge number of coroutines executed in parallel. That's unlikely to be what you want. If you want to ...


3

Coroutines are a strange beast. Yield return causes the method to suspend execution until it is later stepped. Behind the scenes, it might look something like this: class FireContinuouslyData { int state; bool shouldBreak; } object FireContinuously(FireContinuouslyData data) { switch (data.state) { case 0: goto State_0; }...


2

IEnumerators (more specifically, yield return) are special and magic pieces of syntax sugar. What is happening is that the compiler creates (at compile time) a class that packages all the state of the function you wrote. And then substitutes the function with a stub that creates an instance of the class and returns it. So you're not running your code in ...


2

yield return StartCoroutine(yourIEnumatorMethod()); Will wait until your method has completed, the only inconvenient with that solution is it forces you to make this call within an IEnumator method. See http://docs.unity3d.com/ScriptReference/Coroutine.html


2

Why not store the isExecuting variable within the action? public class CharacterBehaviour: MonoBehaviour { Action action; void Start{ action=new Action(); } void Update{ //Other methods if(!action.isExecuting) { StartCoroutine(action.execute()); } } public class Action: ...


2

B test = new B() is not allowed for Components & MonoBehaviours in Unity; there cannot be constructors. Use something like: test = gameObject.AddComponent<B>();


2

Turned my comment into answer, to make the question answered. The problem lies in the while loop never actually reaching it's condition. Only the position is being manipulated but whole transform equality is being tested and the rotations do not match, obviously. That in return never stops the coroutine and whenever the coroutine is called again, the ...


2

try this, you simply give the WaitForKeyDown coroutine a list of keys to check, once it registers a key, it will exit the checking loop & terminate: using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class InputTester : MonoBehaviour { int choice = -1; // valid choices for this example are 0 and 1 ...


2

The reason why the button gets stuck until the scene loads is, you are not loading the mesh in a parallel thread or a coroutine that allows the game loop to keep ticking, hence, you are loading it in a concurrent environment, which means that the next frame will tick once that mesh (and whatever more resources) finish loading. What you can do is having an ...


2

Unity Coroutines will exit automatically when they reach the end of the function. Thus, the answer is simply to use a for loop instead of a while loop. If you need the number of times it runs for to vary, you can pass this in as a function (see this). For Example: IEnumerator DoSomething(int loops) { for (int idx = 0; idx < loops; idx++) { ...


2

I assume the lines you don't understand are those you put comments on, so I will explain them in order. A method returning an IEnumerator is one which can use the yield instruction to return values to the caller. Whenever the program flow reaches a yield, the program flow goes back to the caller. The caller can then process that value and then resume the ...


2

Remember, when you start a coroutine, your execution doesn't block and wait for it to finish. It continues to the next line immediately. So, the problems in your code are: var bundle = parent.StartCoroutine(_loadOneAssetBundle(full_path)).assetBundle; A) StartCoroutine just starts a Coroutine - it doesn't load your asset bundle for you (it doesn't know ...


2

This (general) approach is used in several AAA engines, but isn't very compatible with the way current Unity (2017) works. To do what you've described, you'd have to build your own task system that doesn't immediately conflict with Unity's own process. Doing so is generally ineffective as the extra overhead negates the benefits of extra time. For further ...


2

Though the comments on this question I was able to find a solution to this. If I transfer it over to the example I posted above it would look like this: //global valiables private const int Speed = 200; //amount of iterations per frame private int skipCount = 0; //counter for the iterations private IEnumerator PropagateSearch(arg0, arg1) { if ((...


2

You need to edit your code something like this: public float time = 5f; private IEnumerator coroutine; public Image image; private float t = 5f; void Start() { image.enabled = false; } private IEnumerator nombredeCorutina(float t, Image im) { yield return new WaitForSeconds(t); image.enabled = true; } public void EnemySpawn() { //call ...


2

GameObject originPlayer = new GameObject(); originPlayer = player; this doesn't do what you think it does. instead you want to save the just the original scale and restore that: if (type == "Bigger") { Vector3 origScale = player.transform.localScale; Debug.Log("make it big"); player.transform.localScale = new Vector3(1.25f, 2.9f, 0); ...


1

First of all: The errors you cite have nothing to do with the problem you're solving - they're just syntax mistakes. When you define delay inside an if like that, the definition isn't visible to the rest of the code - it exists only inside the scope of the if itself (which is just that line defining the variable alone - not very useful, which is why the ...


1

Be careful when running coroutines in this manner. Especially lots of them. I agree with user @Jimmy, who left a comment on your question; however, for the sake of pointing you in the right direction and helping you become better at understanding programming and calling functions, I'll detail a solution below. On your enemyAttack script, add the following ...


Only top voted, non community-wiki answers of a minimum length are eligible