Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by $$anonymous$$ · Apr 18, 2019 at 06:32 PM · worldstars

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);
     }
    }
   }
  
  }
 
 
  }
 
     
 
 
 
     
 
 

Comment
Add comment · Show 14
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Apr 18, 2019 at 06:56 PM 0
Share

??????????

avatar image $$anonymous$$ · Apr 19, 2019 at 05:48 PM 0
Share

help me pleaseeee

avatar image TreyH $$anonymous$$ · Apr 19, 2019 at 05:57 PM 1
Share

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.

avatar image $$anonymous$$ TreyH · Apr 19, 2019 at 06:42 PM 0
Share

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.

avatar image surfuay · Apr 19, 2019 at 06:54 PM 0
Share

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.

avatar image $$anonymous$$ surfuay · Apr 19, 2019 at 07:13 PM 0
Share

I do not understand how to count the total number of stars on which the next world will open

avatar image surfuay $$anonymous$$ · Apr 20, 2019 at 06:03 PM 0
Share

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

Show more comments
avatar image DiegoSLTS · Apr 19, 2019 at 08:10 PM 0
Share

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.

avatar image $$anonymous$$ DiegoSLTS · Apr 20, 2019 at 01:49 PM 0
Share

GameObject star1 can not be added to some variable 1, when GameObject gets star2 - add 2, when GameObject star3 is added 3,?

avatar image DiegoSLTS $$anonymous$$ · Apr 20, 2019 at 01:57 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Apr 22, 2019 at 05:36 PM 0
Share

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.

avatar image highpockets $$anonymous$$ · Apr 22, 2019 at 06:21 PM 0
Share

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..

avatar image $$anonymous$$ highpockets · Apr 22, 2019 at 06:34 PM 0
Share

How can I add stars in my code?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dynamic world in Unity 4 Answers

iTween MoveAdd world coordinates Simple 2 Answers

Do I need a license when using models of real world things (cars, buildings etc)? 2 Answers

How to move Character along world floor? 2 Answers

Convert world space to GUI Rect? 5 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges