Levels Unlock Experiment

Hi, I am experimenting with an unlock system for my game, but something is very wrong because when I run my script all my levels unlock themselves. I would be grateful for some pointers. Many thanks!

 using UnityEngine;
 using System.Collections;
 
 public class LevelsManager : MonoBehaviour {
 
     public GameObject LockedLevel2;
     public GameObject LockedLevel3;
     public GameObject LockedLevel4;
     public GameObject LockedLevel5;
     public GameObject LockedLevel6;
     public GameObject LockedLevel7;
     public GameObject LockedLevel8;
     public GameObject LockedLevel9;
     public GameObject LockedLevel10;
     public GameObject LockedLevel11;
     public GameObject LockedLevel12;
     private int IsReached;
 
 
 
     void Start () {
 
         LockedLevel2.SetActive (true);
         LockedLevel3.SetActive (true);
         LockedLevel4.SetActive (true);
         LockedLevel5.SetActive (true);
         LockedLevel6.SetActive (true);
         LockedLevel7.SetActive (true);
         LockedLevel8.SetActive (true);
         LockedLevel9.SetActive (true);
         LockedLevel10.SetActive (true);
         LockedLevel11.SetActive (true);
         LockedLevel12.SetActive (true);
 
         Assign (); 
 
     }
 
         void Assign ()
     {
 
         IsReached = PlayerPrefs.GetInt ("Level Completed");
         print (IsReached);
 
         IsReached = 1;
         LockedLevel2.SetActive (false);
             
 
         IsReached = 2;
         LockedLevel3.SetActive (false);
 
             
         IsReached = 3;
         LockedLevel4.SetActive (false);
 
 
         IsReached = 4;
         LockedLevel5.SetActive (false);
 
 
         IsReached = 5;
         LockedLevel6.SetActive (false);
 
 
         IsReached = 6;
         LockedLevel7.SetActive (false);
 
 
         IsReached = 7;
         LockedLevel8.SetActive (false);
     
 
         IsReached = 8;
         LockedLevel9.SetActive (false);
 
 
         IsReached = 9;
         LockedLevel10.SetActive (false);
 
 
         IsReached = 10;
         LockedLevel11.SetActive (false);
 
 
         IsReached = 11;
         LockedLevel12.SetActive (false);
 
 
 
         }    
     
 }

IsReached = 1;
LockedLevel2.SetActive (false);

IsReached = 2;
LockedLevel3.SetActive (false);

What I guess you want to do is something like this:

if (IsReached < 1)
    LockedLevel2.SetActive (false);

if (IsReached < 2)
    LockedLevel3.SetActive (false);

And so on for all of your game objects.

Many thanks for your reply. What this does is unlock all my levels apart from the one that is due for unlocking. However, if I use ==, then all levels are locked apart from the one that is due to be unlocked. What I would want is for exactly that to happen, but also for the previous levels to remain unlocked. Here is the amended code:

using UnityEngine;
using System.Collections;

public class LevelsManager : MonoBehaviour {

	public GameObject LockedLevel2;
	public GameObject LockedLevel3;
	public GameObject LockedLevel4;
	public GameObject LockedLevel5;
	public GameObject LockedLevel6;
	public GameObject LockedLevel7;
	public GameObject LockedLevel8;
	public GameObject LockedLevel9;
	public GameObject LockedLevel10;
	public GameObject LockedLevel11;
	public GameObject LockedLevel12;
	private int IsReached;


		void Start ()
	{

		IsReached = PlayerPrefs.GetInt ("Level Completed");
		print (IsReached);

		if (IsReached == 1)
		LockedLevel2.SetActive (false);
			

		if (IsReached == 2)
		LockedLevel3.SetActive (false);

			
		if (IsReached == 3)
		LockedLevel4.SetActive (false);


		if (IsReached == 4)
		LockedLevel5.SetActive (false);


		if (IsReached == 5)
		LockedLevel6.SetActive (false);


		if (IsReached == 6)
		LockedLevel7.SetActive (false);


		if (IsReached == 7)
		LockedLevel8.SetActive (false);
	

		if (IsReached == 8)
		LockedLevel9.SetActive (false);


		if (IsReached == 9)
		LockedLevel10.SetActive (false);


		if (IsReached == 10)
		LockedLevel11.SetActive (false);


		if (IsReached == 11)
		LockedLevel12.SetActive (false);

		}	
	
}

I hope this makes sense. If anyone could tell me how to get the previously unlocked levels to remain unlocked as well, I would be very grateful. If I find the answer on my own, I’ll post it here.

Ok, I have found the answer!!!

if (IsReached >= 1) {
			LockedLevel2.SetActive (false);
		}


		if (IsReached >= 2) {
			LockedLevel3.SetActive (false);
		}


		if (IsReached >= 3) {
			LockedLevel4.SetActive (false);
		}

etc.

So many hours… I hope it will help someone one day! :slight_smile: