🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Infinite Loops and State Machines

Started by
1 comment, last by Kk1496 8 years, 10 months ago

How do you fix infinite loops

These are the different game states


void Update ()
{
	switch (_currentState) {
	case GameState.start:
		Debug.Log("Start");
		_currentState = GameState.inGame;
		break;
	case GameState.inGame:
		//Debug.Log("In Game");
		_scoreText.text = "Score: " + _score;
		break;
	case GameState.gameOver:
		Debug.Log("Game Over");			_scoreText.enabled = false;
		_gameOverText.enabled = true;
		break;		}
	}
}

This loops seems to be causing unity to crash


while (GameManager.gameManager.CurrentState == GameManager.GameState.inGame) 
{
	Debug.Log("Enter Lop");
	transform.position = Vector3.Lerp(targetPoint1.position, targetpoint2.position, time);
	if (Input.GetKey(KeyCode.Space))
	{
		GameManager.gameManager.CurrentState = GameManager.GameState.gameOver;
	}
}

I have aa means of breaking out of the loop so I'm not sure why unity is crashing

Advertisement

I dont know much about unity, but probably the issue is not that the loop is infinite, but that it never pauses so that input and other things can be processed.

Assuming such an infinite loop is the ideal way to handle constant updating in unity (look at some other peoples code to make sure you got it right) - is there some function in unity to pause the loop so it doesnt hog all the cpu?

this looks like it might work:

http://docs.unity3d.com/ScriptReference/WaitForSeconds.html

(eg call it once in the loop to wait a small time, so the loop runs at a finite rate, like ~30 FPS or whatever you want)

but again, make sure this is the correct way to handle things in unity. Are you sure you cant just put that code in the loop, into the "Update" method? (without the loop)

o3o

I've tried using an iEnumerator but the sprite doesn't seem to move bettween the two points.


using UnityEngine;
using System.Collections;

public class EnemyStepController : MonoBehaviour 
{
	//Points to travel to and from
	public Transform[] targetPoints;

	public int time = 5;

	public IEnumerator StepMovement ()
	{
		for (int cnt = 0; cnt < targetPoints.Length; cnt++) 
		{
			transform.position = Vector3.Lerp (transform.position, 
			                                   targetPoints[cnt].position, 
			                                   time * Time.deltaTime);
		}

		if (Input.GetKey(KeyCode.Space))
		{
			GameManager.gameManager.CurrentState = GameManager.GameState.gameOver;
			yield break;
		}
	}

	void OnTriggerEnter2D (Collider2D col)
	{
		if (col.tag == "Player")
		{
			GameManager.gameManager.CurrentState = GameManager.GameState.gameOver;
		}
	}
}

Also, whenever I change the time variable in the inspector i get 3 of these errors

Unsupported type vector

This topic is closed to new replies.

Advertisement