Search Unity

Any better way to code this rather than the if statements?

Discussion in 'Scripting' started by xXAl-HarereXx, Aug 10, 2020.

  1. xXAl-HarereXx

    xXAl-HarereXx

    Joined:
    Aug 21, 2017
    Posts:
    101
    I want to change the stats of enemies mid game depending on the player score... so i'm checking if the score is more than the required score for each enemy on each frame using the update method. This is a bad solution for it and I want to know if there is any better ones. Thanks :)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyStateChanger : MonoBehaviour
    6. {
    7.     [Header("Normal Parrot Limit")]
    8.     [SerializeField] Enemy normalParrot;
    9.     [SerializeField] int normalParrotOriginalSpawnLimit;
    10.     [SerializeField] int normalParrotMidGameSpawnLimit;
    11.     [SerializeField] int normalScoreRequiredToChangeLimit;
    12.  
    13.     [Header("Fast Parrot Speed")]
    14.     [SerializeField] Enemy fastParrot;
    15.     [SerializeField] float fastParrotOriginalSpeed;
    16.     [SerializeField] float fastParrotNewSpeed;
    17.     [SerializeField] int fastParrotScoreRequiredToChangeSpeed;
    18.  
    19.     [Header("Big Parrot Limit")]
    20.     [SerializeField] Enemy bigParrot;
    21.     [SerializeField] int bigParrotOriginalSpawnLimit;
    22.     [SerializeField] int bigParrotMidGameSpawnLimit;
    23.     [SerializeField] int bigParrotLateGameSpawnLimit;
    24.     [SerializeField] int bigMidScoreRequiredToChangeLimit;
    25.     [SerializeField] int bigLateScoreRequiredToChangeLimit;
    26.  
    27.  
    28.     [Header("Fluff Cannon Limit")]
    29.     [SerializeField] Enemy fluffCannon;
    30.     [SerializeField] int fluffCannonOriginalSpawnLimit;
    31.     [SerializeField] int fluffCannonMidGameSpawnLimit;
    32.     [SerializeField] int fluffCannonLateGameSpawnLimit;
    33.     [SerializeField] int fluffMidScoreRequiredToChangeLimit;
    34.     [SerializeField] int fluffLateScoreRequiredToChangeLimit;
    35.  
    36.     private void Start()
    37.     {
    38.         fluffCannon.setSpawnLimit(fluffCannonOriginalSpawnLimit);
    39.         normalParrot.setSpawnLimit(normalParrotOriginalSpawnLimit);
    40.         fastParrot.GetComponent<MoveObject>().setSpeed(fastParrotOriginalSpeed);
    41.         bigParrot.setSpawnLimit(bigParrotOriginalSpawnLimit);
    42.     }
    43.  
    44.     private void Update()
    45.     {
    46.         if (ScoreManager.currentScore >= fluffMidScoreRequiredToChangeLimit)
    47.             fluffCannon.setSpawnLimit(fluffCannonMidGameSpawnLimit);
    48.  
    49.         if (ScoreManager.currentScore >= fluffLateScoreRequiredToChangeLimit)
    50.             fluffCannon.setSpawnLimit(fluffCannonLateGameSpawnLimit);
    51.  
    52.         if (ScoreManager.currentScore >= normalScoreRequiredToChangeLimit)
    53.             normalParrot.setSpawnLimit(normalParrotMidGameSpawnLimit);
    54.  
    55.         if (ScoreManager.currentScore >= bigMidScoreRequiredToChangeLimit)
    56.             bigParrot.setSpawnLimit(bigParrotMidGameSpawnLimit);
    57.  
    58.         if (ScoreManager.currentScore >= bigLateScoreRequiredToChangeLimit)
    59.             bigParrot.setSpawnLimit(bigParrotLateGameSpawnLimit);
    60.  
    61.         if (ScoreManager.currentScore >= fastParrotScoreRequiredToChangeSpeed)
    62.             fastParrot.GetComponent<MoveObject>().setSpeed(fastParrotNewSpeed);
    63.     }
    64. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    You could use events. Add a ScoreChanged event to the ScoreManager. Have each enemy subscribe to the event and update their own spawn limits in response to that event.
     
    Brathnann likes this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    First I wouldn't do this in Update. Since the requirement is to switch based on the currentScore, I would simply set up an event where when currentScore changes, check if you need to change the enemy stats. There are ways to use formulas to calculate what should change, but from looking at your if statements, you aren't always changing the same enemies.

    So with that in mind, the only other thing is you could use a switch statement if you wanted.

    I would also suggest changing to a if else statement with the highest if statement at the top if you just want to stick with if statements unless you plan to always increase all enemy stats.

    Otherwise, in Update, it will keep triggering every frame, which may not be your intended effect.