Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cassandrasCurse · May 11 at 12:49 PM · updatespawnspawningcoroutinesspawning problems

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

143 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning help 1 Answer

Navmesh problem with SpawnSystem 0 Answers

How to spawn enemies at different locations and avoid overlapping each other 2 Answers

How to prevent multiple spawns from a single spawn point? 1 Answer

How to increase the spawn rate of an object over time? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges