0

I have two tanks that both share a health script, and each tank is differentiated by a public variable. My issue is that when one tank health reaches 0, the onDeath section of the script will play, but only for that tank. There is important code that affects both tanks in the onDeath section, so I would like to be able to make the onDeath section play for both tanks when one dies. The current code looks something like this:

if (tankhealth >= 0) {
    onDeath ();
}

With the code like this currently only the tank that has 0 health will play the onDeath section, but I would like the code to look something like this:

if (tankhealth >= 0) {
    for (i = 0; i =2; i++) {
        playernumber[i]
        onDeath ();
    }
}

but im not sure how to connect the two, and any way that i try to do this i get an error saying that the brackets arent allowed. Any way to connect this and make it work properly?

4
  • for (i = 0; i =2; i++) should be for (int i = 0; i < 2; i++) for a start. Commented Jan 19, 2017 at 13:25
  • for (i = 0; i < playernumber.Length; i++) as @Ben says below is better still, avoiding "magic numbers" and making the intent clearer. Commented Jan 19, 2017 at 13:44
  • Is tankhealth an int or float? Commented Jan 20, 2017 at 6:35
  • tankhealth is a float Commented Jan 20, 2017 at 15:51

1 Answer 1

1

Why is the OnDeath() method ran when the tankhealth is more than 0?

Perhaps i'm making too many assumptions but...

if (tankhealth >= 0) {
    for (i = 0; i < playernumber.Length; i++) {
        playernumber[i].onDeath();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

sorry didnt realize that i messed up my angle brackets that i used when i wrote the post. In the script it does have the onDeath() running when tankhealth is less than 0. I checked the code that you wrote and the current error message is cs1061 and says that type 'int' does not contain a definition for 'length' and no extension method 'length' of type 'int' could be found. The same message pops up for onDeath() instead of length
What type is playernumber? I assumed that it is an array, since your original post accesses it as such. i.e. int[] playernumber = new int[2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.