Open Worlds on the basis of stars

I want to open the next world on the basis of the number of stars.

What kind of number of stars can be counted accordingly?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class CoinScript : MonoBehaviour {

	public static float scoreCoin;
	/////////////////////////
	public float astx_1;
	public float astx_2;
	public float astx_3;
	/////////////////////////
	public  float winCoins;
	public float coin_num;

	/////////////////////////
	public static float ballon_1;
	/////////////////////////
	public GameObject Panel_youwin;
	public GameObject Panel_youlose;


	Text coinscore;
	
 public GameObject star1;
 public GameObject star2;
 public GameObject star3;
 
 //reference to next button

  
	protected string currentLevel;
	protected int worldIndex;
	protected int levelIndex;
	public static bool isLevelComplete ;


	// Use this for initialization
	void Start () {
		
		isLevelComplete = false;
	//get the star images

	//disable the image component of all the star images
	star1.GetComponent<Image>().enabled = false;
	star2.GetComponent<Image>().enabled = false;
	star3.GetComponent<Image>().enabled = false;

	
	//disable the next button
	//save the current level name
	currentLevel = Application.loadedLevelName;

		ballon_1=0;
		scoreCoin = coin_num;
		coinscore = GetComponent<Text>();

		Panel_youwin.SetActive(false);
		Time.timeScale = 1f;

		Panel_youlose.SetActive(false);
		Time.timeScale = 1f;
	

	}

	void Update () {
		if (coinscore != null){
			
		coinscore.text = "Score " + scoreCoin;
		}
		
	if(ballon_1==astx_3&&Health.healthlife==3){
		star3.GetComponent<Image>().enabled = true;
  	  	UnlockLevels(3);
		Panel_youwin.SetActive(true);
		Time.timeScale = 0f;
	}

	if(ballon_1==astx_2&&Health.healthlife==2){
		star2.GetComponent<Image>().enabled = true;
		UnlockLevels(2);
		Panel_youwin.SetActive(true);
		Time.timeScale = 0f;	
	}

	if(ballon_1==astx_1&&Health.healthlife==1){
		star1.GetComponent<Image>().enabled = true;
    	UnlockLevels(1);
		Panel_youwin.SetActive(true);
		Time.timeScale = 0f;
	}

	if(Health.healthlife <= 0){
		Panel_youlose.SetActive(true);
		Time.timeScale = 0f;
		
		}	
	}
	 protected void  UnlockLevels (int stars){
 
  //set the playerprefs value of next level to 1 to unlock
  //also set the playerprefs value of stars to display them on the World levels menu
  for(int i = 0; i < LockLevel.worlds; i++){
   for(int j = 1; j < LockLevel.levels; j++){               
    if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
     worldIndex  = (i+1);
     levelIndex  = (j+1);
     PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
     //check if the current stars value is less than the new value
     if(PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +j.ToString()+"stars")< stars)
      //overwrite the stars value with the new value obtained
      PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +j.ToString()+"stars",stars);
    }
   }
  }
 
 }


 }

Ok, I get what you want. You should get this done with player prefs, but during gameplay it might be a better idea to just have a starCount int variable.


So, if the PlayerPrefs int already exists, give starCount that value at the start of the scene and every time you collide with a star collider (I’m not sure how you account for collecting stars, but I assume either OnCollisionEnter or OnTriggerEnter is what you are using) add to starCount. When you exit the scene (OnDestroy()) set the levelStarCount to starCount. If you always use this script, it will get/create the unique key for the scene in question:


int starCount;
string levelStarCount;

void Start()
{

levelStarCount = SceneManager.GetActiveScene().name + “StarCount”;
if(PlayerPrefs.HasKey(levelStarCount))
{
starCount = PlayerPrefs.GetInt(levelStarCount);
}
else
{
starCount = 0;
}
}

void OnCollisionEnter(Collider other)
{

if(other.tag == “Star”)
{
starCount = starCount + 1;
}
}

void OnDestroy()
{
PlayerPrefs.SetInt( levelStarCount, starCount );
}

I did not test the code, but it should do the trick. Note that this script would need to be attached to the player as when it collides with the game object with the tag “Star” it adds to starCount.


Hope that helps, Cheers