- Home /
Activating all powerups at once is glitching my powerups
So I have an ad reward that activates powerups on my player. I'll post below but here's what's happening.
watch the ad activate powerups shield works ninja star does not after ONLY the power up video the ninja star stops working altogether. It doesn't crash the game but that specific powerup now remains in the "on" position AND does not throw the ninja stars despite be "on"
Player Code For the powerups;
public class Player : MonoBehaviour {
public static Player ninja;
public bool NinjaStarOn = false;
public bool ShieldsOn = false;
//not working fine
public void NinjaStar()
{
NinjaStarOn = true;
StartCoroutine(NinjaStarPowerDown());
}
//works fine
public void NinjaShield()
{
ShieldsOn = true;
_ninjaShield.SetActive(true);
StartCoroutine(NinjaShieldPowerDown());
}
}
And here is the Ad script that calls for the powerups to activate, both get activated yet, only the ninja shield functions properly public class ChestPowerAd : MonoBehaviour {
public void ShowAd()
{
var options = new ShowOptions();
options.resultCallback = HandleShowResult;
Advertisement.Show(placementID, options);
}
void HandleShowResult(ShowResult result)
{
if (result == ShowResult.Finished)
{
Debug.Log("reward");
Player.ninja.NinjaStar();
Player.ninja.NinjaShield();
UIManager.uIManager.rewardChestPowerButton.SetActive(false);
UIManager.uIManager.rewardChestPowerVis = false;
UIManager.uIManager.pauseMenuPanel.SetActive(true);
UIManager.uIManager.pauseMenuVisible = true;
}
}
}
i'm not sure what you mean by returning but in the coroutines there is an "off" function
//this is the power up that is working IEnumerator NinjaShieldPowerDown() {
yield return new WaitForSeconds(3); _ninjaShield.SetActive(false); ShieldsOn = false;
}
//this is the one that isn't working IEnumerator NinjaStarPowerDown() { yield return new WaitForSeconds(3); NinjaStarOn = false; }
when i get the power ups in normal gameplay they function perfectly.
oh, and the methods (Player.ninja.NinjaStar()/NinjaShield()) both call those coroutines in those methods
I meant calling in your coroutines to return some value or action. Have you tried nesting them? You could try running them in parallel in your Player script with something like this:
private Coroutine shield, star;
private IEnumerator powerDown;
private bool down;
void Start()
{
down = false;
shield = StartCoroutine(NinjaShieldPowerDown());
star = StartCoroutine(NinjaStarPowerDown());
powerDown = PowerDown();
}
IEnumerator PowerDown()
{
down = true;
yield return WaitForSeconds(3f):
if(shield == null && ShieldsOn){ yield return shield;}
if(star == null && NinjaStarOn){ yield return star;}
down = false;
}
IEnumerator NinjaShieldPowerDown()
{
ninjaShield.SetActive(false);
ShieldsOn = false;
}
IEnumerator NinjaStarPowerDown()
{
NinjaStarOn = false;
}
Then call powerDown in your NinjaStar and NinjaShield functions:
public void NinjaStar()
{
NinjaStarOn = true;
if(!down){StartCoroutine(powerDown);}
}
public void NinjaShield()
{
ShieldsOn = true;
_ninjaShield.SetActive(true);
if(!down){StartCoroutine(powerDown);}
}