Levels for my arcade game

Hello. I have a really basic arcade game and i want to do some challenges for my game. All tutorials online are teaching the unlocking level system. But the thing is this is an arcade game so all levels are unlocked. I want to put a checkmark if the level is cleared. So do i have to write 15 times(there are 15 levels) the same code with a different number 15 times? And also the checkmark feature, do i have to create 15 PlayerPrefs integers and set them to 1 and the check if it is 1 15 times? Isn’t there an easier and quicker way?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class challenges : MonoBehaviour
{
    public GameObject[] texts;
    public GameObject[] checkmarks;
    private void Start()
    {
       
    }
    public void Update()
    {
      
    }
    public void challenge(int button)
   {
     
        if (button == 1)
        {
           
        
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 2)
        {
           
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 3)
        {
       
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 4)
        {
         
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 5)
        {
          
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 6)
        {
       
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 7)
        {
     
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 8)
        {
        
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 9)
        {
         
            SceneManager.LoadScene("challenge " + button.ToString());
        }
   
        if (button == 10)
        {
        
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 11)
        {
          
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 12)
        {
          
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 13)
        {
        
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 14)
        {
         
            SceneManager.LoadScene("challenge " + button.ToString());
        }
        if (button == 15)
        {
         
            SceneManager.LoadScene("challenge " + button.ToString());
        }
       
    }
}

Your challenge method can just be this and it will work the same:

public void challenge (int button) {
  SceneManager.LoadScene("challenge " + button.ToString());
}

Your if statements are pointless… The code in each is the same. If you ever find that you are writing the same or similar code multiple times, the answer is always yes you can do it more simply. PlayerPrefs is not the best way to save game data but no you don’t have to write the same code for each number. I suggest going through some C# tutorials to learn how to use “for loops” and “while loops”. Those will let you go through all of your levels and run the same code (and only write the code once).

1 Like

All i had to do was a for loop and works like a charm. Thanks! I can’t believe how dumb i was.