- Home /
Number of stars
Hi!
I want to see the number of stars in the level below every world.
Besides the victory panel, I want to dynamically locate the results of the stars on the levels.
When I win the first level , the same number of stars are shown below the numbers of all the levels.
I want to dynamically see the number of stars in each level
This is the code for the stars
public class Stars : MonoBehaviour {
public static int stars;
public static int allstars;
public Image[] starphoto;
public Sprite fullstar;
public Sprite emptystar;
void Start(){
stars = stars;
allstars = 3 ;
}
void Update(){
if(stars > allstars){
stars = allstars;
}
for (int i = 0; i < starphoto.Length; i++){
if (i < stars){
starphoto[i].sprite = fullstar;
} else {
starphoto[i].sprite = emptystar;
}
if (i < allstars){
starphoto[i].enabled = true;
} else {
starphoto[i].enabled = false;
}
}
}
}
Your question isn't very clear or specific, are you asking how to position the stars?
yes, Besides the victory panel, I want to dynamically locate the results of the stars on the levels.
This code works for the stars, I want to see the stars in every world underneath the world, how it is done?
so are you trying to see how many stars are available in the current level or the total number of stars you've collected thus far and retain a running tally regardless of the level you're in
Besides the victory panel, I want to dynamically locate the results of the stars on the levels.
This code works for the stars, I want to see the stars in every world underneath the world, how it is done?
Parent the stars to an empty gameObject then attach the script to that parent object and drag it to the assets to create a prefab of it, you can then use it in any level you want by dragging it to the hierarchy or instantiate it by a script.
Answer by xxmariofer · Apr 03, 2019 at 07:12 PM
Change the int all stars to an array of ints, since you need to compare the amount of stars per level, you will need to add the stars value depending in the current scenebuildindex and just iterate over the array for displaying the stars
Answer by surfuay · Apr 03, 2019 at 07:37 PM
before you get to crazy on trying to recode anything, 1 problem that may be preventing you from showing the running tally of stars accrued is that your statement
stars = allstars
is backwards.
so your stars = stars is fine, allstars = 3 is fine but in the update method what's happening is you are equating stars to allstars anytime stars is greater than allstars, so its resetting the displayed int back at 3 everytime. the simple fix would be allstars = stars.
obviously in math on paper A = B is the same as B = A but in coding what's happening is that it reads the first variable and equates it to the second rather than making which ever is higher the master variable.
let me know if that works, if not there may be a few other things to add that I think may make it work
ok try allstars = all stars + stars;
that should on each update allow for your allstars int to change.
so in the level you'll continue just to have whatever stars you collected there but your allstars location will have the previous total plus your new total.
only hitch I can think is it may just start adding the stars infinitely since you call the function every frame.
Your answer
Follow this Question
Related Questions
how to give star score once per each level? 0 Answers
Dynamic Star System with PlayerPrefs 1 Answer
How to make PlayerPrefs delete if the player didn't touch game finish collider 1 Answer
Collestion of objects 3 Answers
Multilevel shooter? 2 Answers