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
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;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613041235im_/https://answers.unity.com/themes/thub/images/avi.jpg)