- Home /
Help with 3 star reward system
I am trying to make a 3 star rating system for a project like angry birds. In my case instead of stars as rewards I use cars.
I have 2 buttons on the scene Level1 and Level2
and four images for the star(car) rewards OneCarSprite TwoCarSprite ThreeCarSprite NoCarSprite
I am using Playmaker to pass the following variables to a script.
LevelToReward ( which holds a string with the name of the level to be rewarded Level1 or Level2 )
and carscore (which is the number of stars(cars) the level will be awarded.
I attach the script to each Buttons (Level1 and Level2)
My problem is that the script works for button1 and when i click on button2 it works but I loose the reward that was assigned to button1
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using HutongGames.PlayMaker;
public class CarsRewardSystem : MonoBehaviour {
public Sprite OneCarSprite;
public Sprite TwoCarSprite;
public Sprite ThreeCarSprite;
public Sprite NoCarSprite;
public string LevelToReward;
Image images; // declare of Image type
public Button l1; // declare of Button type
public Button l2;
// Use this for initialization
void Start () {
LevelToReward = FsmVariables.GlobalVariables.GetFsmString("levelCompleted").Value; // get variable from Playmaker FSM
}
// Update is called once per frame
void Update () {
}
void Cars(int carscore) {
Debug.Log (LevelToReward);
Debug.Log (carscore);
// changes the sprites to allow rewards of 1 , 2, 3 cars according to score
images = gameObject.GetComponent<Image>(); // get the component of Image method
l1 = gameObject.GetComponent<Button>(); // get the component of Button method.
l2 = gameObject.GetComponent<Button>(); // get the component of Button method.
if (carscore == 1 && LevelToReward == "Level1") {
//images.sprite = OneCarSprite;
l1.image.sprite = OneCarSprite;
} else if(carscore == 2 && LevelToReward == "Level1") {
//images.sprite = TwoCarSprite;
l1.image.sprite = TwoCarSprite;
} else if(carscore == 3 && LevelToReward == "Level1") {
//images.sprite = ThreeCarSprite;
l1.image.sprite = ThreeCarSprite;
} else if (carscore >3 || carscore<1){
l1.image.sprite = NoCarSprite;
}
if (carscore == 1 && LevelToReward == "Level2") {
//images.sprite = OneCarSprite;
l2.image.sprite = OneCarSprite;
} else if(carscore == 2 && LevelToReward == "Level2") {
//images.sprite = TwoCarSprite;
l2.image.sprite = TwoCarSprite;
} else if(carscore == 3 && LevelToReward == "Level2") {
//images.sprite = ThreeCarSprite;
l2.image.sprite = ThreeCarSprite;
} else if (carscore >3 || carscore<1){
l2.image.sprite = NoCarSprite;
}
}
}
Answer by GluedBrain · Nov 01, 2014 at 04:09 AM
Check this article which covers exactly how to do this from scratch..
Answer by thelime · Oct 17, 2014 at 10:56 AM
Lets see if i understand you. You play level 1 with button 1 and when done you get the rewards? And after you play level 2 with button 2 and you are done you get the rewards but the result from level 1 is missing? If it is like that you have the problem here
} else if (carscore >3 || carscore<1){
l1.image.sprite = NoCarSprite;
// you dont checking if it is level1 you have played so it will always change it to noCarSprite
add this to
&& LevelToReward == "Level1"
and it should work :)
That's my problem yes. I will check your solution in a couple of hours. Thanks for your time :)
okey if it still is problem post it here and i will try to help you!
no luck it is not working. At other parts of my code I am using PlayerPrefs to save and load the rewards of each level. $$anonymous$$aybe there is the problem. If this part of code is correct.
yes maybe. if you whant more help you need to post more kode and show how you save and loade data
Your answer
Follow this Question
Related Questions
[4.6 - UI] Calling function on button click via script 1 Answer
UI 4.6 Image blocks Mouse Click 0 Answers
9 Sliced Image Is Bleeding Edges 0 Answers
Multi Coloured Buttons 0 Answers
[Solved]Why my Unity Button's Positions are different. 1 Answer