- Home /
Does using both Update() and a coroutine cause respawning problems?
Hi everyone, I've been working on my first game which uses mechanics similar to whack-a-mole. In my prototype, you're trying to tap as many squares as possible, on a conveyor belt of sorts, under certain conditions.
Some things work as expected, like when a square reaches the edge of the block outside of the block's bounds, it is re-positioned back to the start of the chain.
Here's the expected sequence of behaviors:
1) At the start of the game, a set of squares are randomly colored and placed in a row
2) If the user clicks on any square, the square disappears
3) Once the square disappears, its slot is "open" and a new square can be added in its place
4) A new square is added to the empty slot, but in a different, randomized color
Where I run into issues is around steps 3-4. When a new square gets added, sometimes it's placed in the right spot automatically. Other times, it glitches out and is placed on top of another object that is at the start of the line. I'm not sure why this is happening but wouldn't be surprised if I was missing the obvious...
Here's a video of the issue: Video link
1) When I click on the orange square, it functions properly
2) When I click on the magenta and the blue square that follows after, they glitch out then restart towards the beginning of the line in the wrong place
Any bit of help would be much appreciated!
Here's my code (it's such a mess, my apologies in advance):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chef : MonoBehaviour
{
public GameObject firstPlateSlot;
public GameObject secondPlateSlot;
public GameObject thirdPlateSlot;
public GameObject fourthPlateSlot;
public GameObject fifthPlateSlot;
public GameObject platePrefab;
GameObject firstPlate;
GameObject secondPlate;
GameObject thirdPlate;
GameObject fourthPlate;
GameObject fifthPlate;
GameObject[] emptyPlatesArray;
public static bool[] platesArray = {false, false, false, false, false};
public static bool isOutOfRightBounds = false;
public static int indexOutOfRightBounds;
void Awake()
{
emptyPlatesArray = new GameObject[] { firstPlateSlot, secondPlateSlot, thirdPlateSlot, fourthPlateSlot, fifthPlateSlot };
firstPlate = Instantiate(platePrefab) as GameObject;
secondPlate = Instantiate(platePrefab) as GameObject;
thirdPlate = Instantiate(platePrefab) as GameObject;
fourthPlate = Instantiate(platePrefab) as GameObject;
fifthPlate = Instantiate(platePrefab) as GameObject;
}
void Start()
{
for (int i = 0; i < platesArray.Length; i++)
{
SpawnPlate(i);
}
StartCoroutine( DelaySpawningPlate() );
}
void Update()
{
if (isOutOfRightBounds)
{
emptyPlatesArray[indexOutOfRightBounds].transform.position = new Vector3 (-9f , 1.0f, 0f);
isOutOfRightBounds = false;
}
firstPlateSlot.transform.position = new Vector3(firstPlateSlot.transform.position.x + .01f, 0.25f, 0f);
secondPlateSlot.transform.position = new Vector3(secondPlateSlot.transform.position.x + .01f, 0.25f, 0f);
thirdPlateSlot.transform.position = new Vector3(thirdPlateSlot.transform.position.x +.01f, 0.25f, 0f);
fourthPlateSlot.transform.position = new Vector3(fourthPlateSlot.transform.position.x +.01f, 0.25f, 0f);
fifthPlateSlot.transform.position = new Vector3(fifthPlateSlot.transform.position.x +.01f, 0.25f, 0f);
firstPlate.transform.position = new Vector3(firstPlateSlot.transform.position.x, 1.05f, 0f);
secondPlate.transform.position = new Vector3(secondPlateSlot.transform.position.x, 1.05f, 0f);
thirdPlate.transform.position = new Vector3(thirdPlateSlot.transform.position.x, 1.05f, 0f);
fourthPlate.transform.position = new Vector3(fourthPlateSlot.transform.position.x, 1.05f, 0f);
fifthPlate.transform.position = new Vector3(fifthPlateSlot.transform.position.x, 1.05f, 0f);
}
IEnumerator DelaySpawningPlate()
{
while(true)
{
for (int i = 0; i < platesArray.Length; i++)
{
yield return new WaitForSeconds(.3125f);
if (platesArray[i] == false)
{
SpawnPlate(i);
}
}
}
}
void SpawnPlate(int index)
{
switch(index)
{
case 0:
firstPlate.SetActive(true);
firstPlate.transform.position = new Vector3 (firstPlateSlot.transform.position.x, 1.0f, 0f);
firstPlate.transform.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
platesArray[index] = true;
firstPlate.tag = "First Plate";
break;
case 1:
secondPlate.SetActive(true);
secondPlate.transform.position = new Vector3 (secondPlateSlot.transform.position.x, 1.0f, 0f);
secondPlate.transform.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
platesArray[index] = true;
secondPlate.tag = "Second Plate";
break;
case 2:
thirdPlate.SetActive(true);
thirdPlate.transform.position = new Vector3 (thirdPlateSlot.transform.position.x, 1.0f, 0f);
thirdPlate.transform.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
platesArray[index] = true;
thirdPlate.tag = "Third Plate";
break;
case 3:
fourthPlate.SetActive(true);
fourthPlate.transform.position = new Vector3 (fourthPlateSlot.transform.position.x, 1.0f, 0f);
fourthPlate.transform.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
platesArray[index] = true;
fourthPlate.tag = "Fourth Plate";
break;
case 4:
fifthPlate.SetActive(true);
fifthPlate.transform.position = new Vector3 (fifthPlateSlot.transform.position.x, 1.0f, 0f);
fifthPlate.transform.GetComponent<Renderer>().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
platesArray[index] = true;
fifthPlate.tag = "Fifth Plate";
break;
}
}
}
[1]: https://drive.google.com/file/d/1Lqpx3ymkF_JplAgIbPhosmo95OE8UBDO/view?usp=sharing