- Home /
How can I use the while loop as a maintenance loop? (e.g.),How can I use the while loop as a maintenance-Loop? (e.g.)
*void Start()
{
MaintenanceLoop();
spawnVector = new Vector2(0, 0);
playgroundSpawner = FindObjectOfType<PlaygroundSpawner>();
blocksBehaviour = FindObjectOfType<BlocksBehaviour>();
blockSpawner = GameObject.Find("BlockSpawner");
if (!blockSpawner)
{
blockSpawner = new GameObject("BlockSpawner");
}
}
void Update()
{
playerVector.y = playgroundSpawner.playerPos.y;
}
public void MaintenanceLoop()
{
while(spawnVector.y == playerVector.y)
{
continue;
}
Spawn();
}
public void Spawn()
{
for (int i = 0; i < 15; i++)
{
if (spawnVector.y == playerVector.y)
{
spawnVector.y += Random.Range(minSpawn, maxSpawn);
Vector2 currentPos = transform.position;
Vector2 yAdjusting = new Vector2(0, 0.5f);
int randValue = Random.Range(0, 3);
if (randValue >= 1)
{
GameObject SmallBlock = Instantiate(smallBlock, transform.position,
Quaternion.identity) as GameObject;
}
else
{
GameObject BigBlock = Instantiate(bigBlock, currentPos + yAdjusting,
Quaternion.identity) as GameObject;
}
}
else
{
MaintenanceLoop();
}
if (i >= 14)
{
Difficulty();
i = 0;
}
}
}
public void Difficulty()
{
if (Random.Range(minSpawn, maxSpawn) > minYSpawnFrequency)
{
minSpawn--;
maxSpawn--;
blocksBehaviour.blockSpeed += 0.25f;
MaintenanceLoop();*
My goal is that when the Player doesnt move the while Loop gets automatically called and works as an maintenance loop until it breaks when the spawnvector is equal the playerPos —> so the for loop can be executed the amount of times and so on
, **void Start() { MaintenanceLoop(); spawnVector = new Vector2(0, 0); playgroundSpawner = FindObjectOfType(); blocksBehaviour = FindObjectOfType();
blockSpawner = GameObject.Find("BlockSpawner");
if (!blockSpawner)
{
blockSpawner = new GameObject("BlockSpawner");
}
}
void Update()
{
playerVector.y = playgroundSpawner.playerPos.y;
}
public void MaintenanceLoop()
{
while(spawnVector.y == playerVector.y)
{
continue;
}
Spawn();
}
public void Spawn()
{
for (int i = 0; i < 15; i++)
{
if (spawnVector.y == playerVector.y)
{
spawnVector.y += Random.Range(minSpawn, maxSpawn);
Vector2 currentPos = transform.position;
Vector2 yAdjusting = new Vector2(0, 0.5f);
int randValue = Random.Range(0, 3);
if (randValue >= 1)
{
GameObject SmallBlock = Instantiate(smallBlock, transform.position,
Quaternion.identity) as GameObject;
}
else
{
GameObject BigBlock = Instantiate(bigBlock, currentPos + yAdjusting,
Quaternion.identity) as GameObject;
}
}
else
{
MaintenanceLoop();
}
if (i >= 14)
{
Difficulty();
i = 0;
}
}
}
public void Difficulty()
{
if (Random.Range(minSpawn, maxSpawn) > minYSpawnFrequency)
{
minSpawn--;
maxSpawn--;
blocksBehaviour.blockSpeed += 0.25f;
MaintenanceLoop();**
My goal is that when the player doesnt move the while Loop gets automatically called and works as an maintenance loop until it breaks when the playerPos is equal the spawn Vector—> so the for Loop can be executed the amount of Times an so on.
Answer by Hellium · Jan 01, 2019 at 06:26 PM
Remove the MaintenanceLoop
and do the check in the Update
, which is ran once per frame
void Start()
{
spawnVector = new Vector2(0, 0);
playgroundSpawner = FindObjectOfType<PlaygroundSpawner>();
blocksBehaviour = FindObjectOfType<BlocksBehaviour>();
blockSpawner = GameObject.Find("BlockSpawner");
if (!blockSpawner)
{
blockSpawner = new GameObject("BlockSpawner");
}
}
void Update()
{
playerVector.y = playgroundSpawner.playerPos.y;
if( !Mathf.Approximately(spawnVector.y, playerVector.y) )
{
Spawn();
}
}
Answer by DownER149 · Jan 01, 2019 at 07:48 PM
StartCoroutine("Setup2"); }
private IEnumerator Setup2()
{
yield return new WaitUntil(() => ***** Your Condition here*****);
**** what you want to happen here*****
}
Your answer
Follow this Question
Related Questions
ArrayList Loop Through - only new elements accessible? 0 Answers
c# loops not working yet no erors 1 Answer
How to destroy numbers after solving them? 2 Answers
Script to Tell if Circuit / Path is Complete 0 Answers
for loop error 1 Answer