Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by yucline · Aug 05, 2021 at 10:14 AM · scripting problemscripting beginnerfor loop

How to make a For Loop that waits until another action is performed before continuing?

Hello,

I have a kind of loot box mechanic in my game, and I want to spawn the items inside the box on top of each other, however, when the objects spawn they will face backwards. The user then needs to click the card which reveals the reward. They then need to click it again and it will go out of the screen.

Once the user has tapped the second time, I would like the 2nd object to spawn in the same location as the first.

At the moment I spawn all the items at once but there is some overlap as they are displayed as canvas' so I would prefer to spawn them one by one.

My current code is:

     public void ShowBoxOpening(Vector3 cardsInitialPosition)
     {
         // 1) Determine rarity of all cards
         RarityOptions[] rarities = new RarityOptions[SlotsForCards.Length];
         bool AtLeastOneRareGiven = false;
         for (int i = 0; i < rarities.Length; i++)
         {
             // determine rarity of this card
             float prob = Random.Range(0f,1f);
             if (prob < LegendaryProbability)
             {
                 rarities[i] = RarityOptions.Legendary;
                 AtLeastOneRareGiven = true;
             }
             else if (prob < EpicProbability)
             {
                 rarities[i] = RarityOptions.Epic;
                 AtLeastOneRareGiven = true;
             }
             else if (prob < RareProbability)
             {
                 rarities[i] = RarityOptions.Rare;
                 AtLeastOneRareGiven = true;
             }
             else
                 rarities[i] = RarityOptions.Common;
         }
 
         if (AtLeastOneRareGiven == false && giveAtLeastOneRare)
         {
             rarities[Random.Range(0, rarities.Length)] = RarityOptions.Rare;
         }
 
         for (int i = 0; i < rarities.Length; i++)
         {
             GameObject card = CardFromPack(rarities[i]);
 
             // each next card will be spawned "under" the previous one in terms of sorting
             card.GetComponentInChildren<Canvas>().sortingOrder = -100 * i;
 
             CardsFromPackCreated.Add(card);
                 card.transform.position = cardsInitialPosition;
                 card.transform.DOMove(SlotsForCards[i].position, 0.5f);
         }
     }
 
     private GameObject CardFromPack(RarityOptions rarity)
     {
         List<CardAsset> CardsOfThisRarity = CardCollection.Instance.GetCardsWithRarity(rarity);
         CardAsset a = CardsOfThisRarity[Random.Range(0, CardsOfThisRarity.Count)];
 
         // add this card to your collection. 
         CardCollection.Instance.QuantityOfEachCard[a]++;
 
         GameObject card;
 
         if (a.TypeOfCard == TypesOfCards.Creature)
         {
             card = Instantiate(CreatureCardFromPackPrefab) as GameObject;
         }
         else
         {
             card = Instantiate(SpellCardFromPackPrefab) as GameObject;
         }
 
         card.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
         OneCardManager manager = card.GetComponent<OneCardManager>();
         manager.cardAsset = a;
         manager.ReadCardFromAsset();
         return card;
     }
 
     public void CreateNextCard()
     {
         StartCoroutine(Waiting());
         
     }
 
     IEnumerator Waiting()
     {
         yield return new WaitForSeconds(2);
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by suIly · Aug 05, 2021 at 11:33 AM

A for loop would lag out the computer. Try using a while loop, like this:

 while(1 = 1)
 {
 if(condition)
 {
 yield break;
 }
 yield return null;
 }

Comment
Add comment · Share
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

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

272 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 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

How to disable raycast/groundchecker on OnTriggerEnter? 0 Answers

script to swap between images using buttons(next button and previous button) 0 Answers

Why is my script not working? 1 Answer

Script problems c# 0 Answers

Script to random set active a child object? 0 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