How to decrease the chance of a power-up occurring?

So i’m trying to make the probability of something happening increase for every enemy i kill, then reset back to base once it happens.

I’ve managed to increase a static float from within a method, but i can’t subtract from it once i add to it for some reason. So i can increase the probability of the thing happening, but i can’t reset it. Trying to work out why.

1 Like

What do you mean by ‘It won’t decrease’ ?
That line: powerUpChance = 0 is never executed?

Seems like that Random.value returns a number in a range from 0 to 1, and most of the time, your powerUpChance is always smaller than generated value because you increment it by 0.01.
Could take you up to 20 - 30 kills until that part of code is executed

1 Like

Hi,

Just so you are aware, you can copy/paste your code directly into the forum, and then simply apply ``` characters before and after it for it to be formatted suitably.


See also;

1 Like

Well, the possibility that I see that could result in that behaviour is if there is an error happening within ShotUp() that don’t even allow the powerUpChance = 0 to run, you could try moving the ShotUp() below that line to see if the still happens.

2 Likes

yeah putting it first did the trick facepalm

thanks guys!

1 Like

yeah it takes a while to generate but i know it was happening because ShotUp(); which spanws a shot power up was happening.
fixed now though, apparently just ordering things differently worked

What is happening inside of your ShotUp() function?

it’s just creating a power up and shooting it downwards

GameObject shotUp = Instantiate(shotUpPrefab, transform.position, Quaternion.identity) as GameObject;
shotUp.GetComponent<Rigidbody2D>().velocity = new Vector3(0, -2, 0);
AudioSource.PlayClipAtPoint(powerUp, transform.position);

could it be because the enemy is destroyed?

There seems to be some kind of error here, otherwise the order shouldn’t matter. Are you sure none of those variables within the Shot method is null?

1 Like

yeah i got it, the audio clip wasn’t plugged in. So the script was fine to look at but when it tried to play it it broke.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms