Hello there!
Right now, my Script should do the following:
Take in the damage from another script. Iterate over all children with health Scripts and distribute the damage in 5 portions randomly among them. If one of them is tagged as a Leader, I’d like it to have a reduced hit chance.
What is happening in my game:
Unity randomly freezes as soon as the damage gets parsed in (like, really random. It works 90% of the time, the rest, unity freezes over without further ado. Also, the leader is getting hit more often then any other corps, evenso unity detects the numbers correctly.
Please, wise fellow coders, look at my code or give me examples how it could solve this differently and I’d be forever grateful!
float chanceToReceiveDamage = 0.1f;
public void takeDamage(float damage) {
damageSplittedAmong = 4;
while (damageSplittedAmong >= 0) {
foreach (Transform Corps in transform) {
if (Corps.GetComponent<CorpsDamageHandler>()) {
if (Corps.tag == "Leader") {
chanceToReceiveDamage = 0.5f;}
if (Random.Range(0, 1f) <= chanceToReceiveDamage) {
Corps.GetComponent<CorpsDamageHandler>().TakeDamage(damage));
damageSplittedAmong -= 1;
}
if (Corps.tag == "Leader") {
chanceToReceiveDamage *= 2f;
}
}
}
We Rock!