Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to figure out how to make this for loop work

Discussion in 'Scripting' started by ArteGen, Jan 23, 2021.

  1. ArteGen

    ArteGen

    Joined:
    Jun 7, 2019
    Posts:
    7
    I'm trying to implement an auto system to upgrade the players ship when they reach a certain score. the goal will increase once its been reached, so I want to the player ship to automatically upgrade every time the next goal has been reached.

    Code (CSharp):
    1.     private void GetPlayerLevel()
    2.     {
    3.         for (int levelIndex = startingLevel; levelIndex < playerConfigs.Count; levelIndex++)
    4.         {
    5.             var currentLevel = playerConfigs[levelIndex];
    6.  
    7.             UpgradePlayer(currentLevel);
    8.         }
    9.     }
    10.  
    11.     private void UpgradePlayer(PlayerConfig playerConfig)
    12.     {
    13.         var newPlayer = Instantiate(playerConfig.GetPlayerPrefab(), playerPosition, Quaternion.identity);
    14.         newPlayer.GetComponent<Player>().SetHealth(playerHealth);
    15.     }
    although I have an idea of how to write for loops I still don't understand how the code is executed.
    The problem that it is giving me is that it immediately cycles through all the levels to the last one. I only want it to execute once for the currentLevel, and then when the next goal has been reached to then upgrade to the next level in the list. I should probably also mention the method is executed in update with an if statement
    alongside updating the goal.

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (score >= upgradeScore)
    4.         {
    5.             UpdateUpgradeScore();
    6.             GetPlayerLevel();
    7.         }
    8.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Then you definitely don't want to be using a for loop. The point of a loop is to execute code many times. You just want to execute it once, so don't use a loop. Just keep an int variable around that keeps track of the current level. Make this code just increment that variable once and do the upgrade.
     
    ArteGen likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You want the ship to upgrade to the relevant what you need to do is map each score to a ship.

    basically something like this, a lot of ways to go about it, you can use a for loop to do it if you're scared of while loops, but definitely not in the way you do it now like @PraetorBlue said.
    Code (CSharp):
    1. public class Ship {
    2.  
    3. public int minScore;
    4. public etc otherStuff;
    5. }
    Code (CSharp):
    1. Ship[] upgradeShips;
    2.  
    3. public GetShipForScore(int score){
    4.  
    5. int i = 0;
    6. while[upgradeShips[i].minScore > score){
    7. i++;
    8. }
    9. return upgradeShips[i];
    10. }
    This is critical, what do you mean by that?
    If you go too deep before learning to swim your gonna drown.
     
    ArteGen likes this.
  4. ArteGen

    ArteGen

    Joined:
    Jun 7, 2019
    Posts:
    7
    Lol, thank you for telling me that. So the reason I was attempting it with a for loop was because I was following a course originally, and they use a for loop to create waves of enemies. when one wave finishes spawning it goes down the list to the next and so on. It enabled me to customize each wave and if I wanted to loop them I could. I thought it would work the same to use the code for the player ships and tweak it, but I couldn't get it to work the same. I didn't realize I was just making it more complicated than it needed to be.
     
  5. ArteGen

    ArteGen

    Joined:
    Jun 7, 2019
    Posts:
    7
    I think I understand it. What was probably confusing me at the time was through trying different methods at the time some code was executing properly and some wasn't, which as you can see there is another method called within that method. Changing the code so much to try to get it to work without realizing what was or wasn't working in the first place just made it over complicated and confusing, but I have fixed it. I just took out the for loop, tied to the current level to a variable and then incremented it when the goal was reached.