- Home /
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);
}
}
}
}
}
You haven't provided any sort of description explaining your problem? You added a few sentences to the question title hinting that you are making a mario64-ish game, but then pasted a large script without any further context.
Help us help you.
I get 3 stars, 2 stars, 1 star. I have 4 worlds, the first of which is open. I want to gather 50 stars in the first world to open the next world. But I do not understand what method they are doing.
this is a fairly large code and i'm not sure where to start BUT there should be a few things you need or a variation there of, this is how i would do it.
public bool World2Open = false; public int stars = 0;
when ever you get a star that int goes up at the appropriate time which i believe you're doing, i'm pretty sure all you're missing is a method like this and a statement in your start script that is checked each time you start your game (which you'll save the bool in player prefs)
void Start()
{
if (World2Open == false)
{
return;
}
else if (World2Open == true)
{
whatever your method for unlocking the world is
}
}
public void OpenWorld2()
{
if (stars >= 50)
{
World2Open = true
}
}
i think those are the only 2 things you're missing. A bool to check before you can access opening the world, and a statement at the start to see if the bool was ever converted to true so that it remains permanently open.
I do not understand how to count the total number of stars on which the next world will open
so, i'm not sure what you're skill level or knowledge base is if you don't understand what i posted. So i'm really sorry if this sounds condescending it's not meant to be.
Watch this tutorial on how to create a "coin" counter (in your case it will be stars).
The basis of this video should give you a way to manipulate your game using the same base for what you want. https://www.youtube.com/watch?v=-EIXQHxoicg
Then watch this video for how to make if statements based on the counter
https://www.youtube.com/watch?v=Pv1Bi6ao_J8
It's about 20 $$anonymous$$utes BUT it sounds like you have a hole in your coding knowledge that these should fix.
I think the big things you're missing is an int counter, a bool and an if statement
Your code is confusing, but I guess I understand how the unlock logic works and it has nothing to do with what you want. The code you posted unlocks a level when you finished the previous one, regardless of the amount of stars you got. Also, it looks like the common star system of lots of mobile games where the stars represent how well you played that level. There's nothing in there that handles other than 1, 2 or 3 stars for a level, nothing that counts the starts, nothing that lets you earn more than 3 stars on the first level.
This makes it really really hard to help you, the script is not intended for what you want to do so it's not something that you could fix in a few lines. If I had to do this, I'll just delete the whole file and write something from scratch. In your case, since it looks like you're learning program$$anonymous$$g (no offense), I'd suggest you search for another script that does something similar to what you want and continue from there.
GameObject star1 can not be added to some variable 1, when GameObject gets star2 - add 2, when GameObject star3 is added 3,?
Of course you can add the stars you earn to a variable, but you have to save that value, add some mechanism to set how many stars unlock each level and actually check those conditions. This is not trivial, I can't tell you how to do that in an answer here. As I said, I recommend you search for another script that does something similar to what you need, this one is not that useful, you'll have to modify most of it to make it work as you want.
Answer by highpockets · Apr 21, 2019 at 08:57 PM
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
I have this code
public class LevelSelectScript : $$anonymous$$onoBehaviour {
private int worldIndex;
private int levelIndex;
//variable which holds the stars obtained
private int stars=0;
void Start (){
//loop thorugh all the worlds
for(int i = 1; i <= LockLevel.worlds; i++){
if(Application.loadedLevelName == "World"+i){
worldIndex = i; //save the world index value
CheckLockedLevels(); //check for the locked levels
}
}
}
//Level to load on button click. Will be used for Level button click event
public void Selectlevel(string worldLevel){
Application.LoadLevel("Level"+worldLevel); //load the level
}
//uncomment the below code if you have a main menu scene to navigate to it on clicking escape when in World1 scene
/*public void Update (){
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape) ){
Application.LoadLevel("$$anonymous$$ain$$anonymous$$enu");
}
}*/
//function to check for the levels locked
void CheckLockedLevels (){
//loop through the levels of a particular world
for(int j = 1; j < LockLevel.levels; j++){
//get the number of stars obtained for that particular level
//used to enable the image which should be displayed in the World1 scene beside the individual levels
stars = PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +j.ToString()+"stars");
levelIndex = (j+1);
//enable the respective image based on the stars variable value
GameObject.Find(j+"star"+stars).GetComponent<Image>().enabled = true;
Debug.Log(j+"-"+stars);
//check if the level is locked
if((PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString()))==1){
//disable the lock object which hides the level button
GameObject.Find("LockedLevel"+levelIndex).SetActive(false);
}
Debug.Log(stars);
}
}
}
It calculates all the stars' amount at the end of a level How can i add all the stars' amount to each other in the end.
The code I gave you above will count the stars during gameplay, you can of course make the string for each level whatever you want. Thereafter when you are in a navigation screen where you select your levels. You can create a variable called totalStars for example and use:
totalStars = totalStars + PlayerPrefs.GetInt(“levelOneWorldOneStarCount”); //use whatever key you use for the level in question
totalStars = totalStars + PlayerPrefs.GetInt(“levelTwoWorldOneStarCount”);
Etc... etc..
Your answer
Follow this Question
Related Questions
Get position of the centre of each grid tile 1 Answer
MMORPG Scripting Ideas 1 Answer
Defining a local spawn offset 1 Answer
Rotating world/player 1 Answer
[C#|JS] GPS Routes? 1 Answer