So - I see there are numerous questions relating to this issue, but I’ve been unable to find one that scratches this particular itch… or if I have, I’m not savvy enough to recognise it. Please be patient - and if you know of a previously answered question that can clear my problem up, please send it right along. =)
I’m executing a while loop which iterates through an array - but as this naturally crashes Unity, I’m trying to execute a coroutine which will slow the execution to once every 4 seconds (in this case).
In theory: The While loop starts, performs the actions in the For section, encounters StartCoroutine(WaitASec (4.0F)); , bounds off to IEnumerator WaitASec, pauses for 4 seconds, then evaluates the While condition and starts again as long as loophandle is true.
In practice: CRASH.
Using Debug.Logs and inserting the line ‘loophandle = false’ at the end of the While loop seems to indicate that the program reaches the Debug.Log line ‘At end of While Loop’ BEFORE the Debug.Log line ‘Waited a sec’.
So - the newbie is baffled.
Right, code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraControllerRevCsharp : MonoBehaviour {
...
//Boolean switch to turn the tracking on and off
public bool loophandle = true;
...
public void Start() {
Transform target = GameObject.Find("Player").transform;
while (loophandle){
Debug.Log ("Started While Loop");
if(cameraNodes.Count > 0){
int node = 0;
for (int i = 0; i < cameraNodes.Count; i++){
Debug.Log ("Started for section");
if(cameraNodes_.range >= Vector3.Distance(target.position, cameraNodes*.position))*_
* {*
* node = i;*
* }*
* }*
* Debug.Log (“About to transform camera”);*
* transform.position = ( (CameraNode)cameraNodes[node]).position;*
* }*
* Debug.Log (“Calling StartCoroutine”);*
* StartCoroutine(WaitASec(4.0F));*
* // FOR TESTING: loophandle = false;*
* Debug.Log (“At end of While Loop”);*
* }*
* }*
* IEnumerator WaitASec(float waitTime){*
* yield return new WaitForSeconds(waitTime);*
* Debug.Log (“Waited a sec”);*
* }*
* public void Update (){*
* transform.LookAt(target);*
* Debug.Log (“Looking at target”);*
* }*
}
Thanks for your brain time on this. Here’s hoping you can phrase the solution in plain enough language so I never end up doing this bloody stupid thing again. =)
–Rev
Again, excellent answer! Much obliged - I've cleared the old errors from my console and this script is working just fine. Naturally, I have a new problem now, related to cs0176... and after a couple of hours trying to solve the issue, it turns out that I can't find an answer on this site. Assuming that it's better to be productive than spin my wheels, I'm going to submit another question and get back to work on Blender. =) Thanks muchly for your time! (I'll just cough leave this here... http://answers.unity3d.com/questions/235285/cs0176-trying-to-locate-the-main-camera-in-order-t.html )
– Reverend-SpeedStill relevant today - you need to use a yield return null in conjunction with the yield return new WaitForXXX. Apparently using just yield return new WaitForXXX variants fails to process. After much debugging the phantom yield return null was the secret here. Great catch!
– StalikVonDark