- Home /
Make a platform disappear after the next one appear
Hi everybody !
I pretty nex to Unity and c# and I'm making a 3D platform game in which I would implement the same mechanic than in HeatMan stage from MegaMan 2. Make platforms disappear as the next appear.
I tried to make my plateforms appear while every other disappear, but it doesn't work correctly.
Here's what I've tried so far :
void Update ()
{
StartCoroutine(MegaManTrap());
}
IEnumerator MegaManTrap()
{
for (int i = 0 ; i < pf.Length ; i++)
{
if (pf[i].activeInHierarchy == false)
{
pf[i].SetActive(true);
}
yield return new WaitForSeconds(Pop);
for (int v = 0 ; v < pf.Length ; v++)
{
if (v != i)
{
if (pf[i].activeInHierarchy == true)
{
pf[i].SetActive(false);
}
}
}
yield return new WaitForSeconds(Depop);
}
}
I don't find the right way to do this, is it possible to have some help plz =) ?
Answer by Jwizard93 · Jul 14, 2017 at 06:16 PM
I don't know yet if the coroutine is written correctly in terms of the logic but you are starting a coroutine every frame so that is surely one problem that must be fixed. See what happens if you start the coroutine once in Start() function instead of Update()
Answer by Zhavina · Jul 14, 2017 at 06:28 PM
Oh, it worked pretty well, every platforms appear and disappear as expected but just once, how can I make the coroutine loop while I play in the start function ?
Ok I solved my problem ! I just reset the variables when all the platforms appeared one time.
I don't know if my code is written correctly but it work as I wanted.
IEnumerator $$anonymous$$ega$$anonymous$$anTrap()
{
for (int i = 0 ; i < pf.Length ; i++)
{
if (pf[i].activeInHierarchy == false)
{
pf[i].SetActive(true);
totalPfActivated++;
}
yield return new WaitForSeconds(Pop);
for (int v = 0 ; v < pf.Length ; v++)
{
if (v != i)
{
if (pf[i].activeInHierarchy == true)
{
pf[i].SetActive(false);
}
}
}
if (totalPfActivated == pf.Length)
{
i = -1;
totalPfActivated = 0;
}
Debug.Log(totalPfActivated);
}
}
Thanks you @Jwizard93
Looks like a fine solution to me. Glad I could help.
Your answer
Follow this Question
Related Questions
Realize toroidal movement 2 Answers
Load Children One at a Time 1 Answer
Make enemy appear in front of player and then disappear at random times 2 Answers
Object appear and disappear on command 1 Answer
Getting For Loops to work in a Coroutine 0 Answers