- Home /
Achievement System
How to make simple achievement system.
Im thinking in making a bool "Achieved = true" after a if statement saying that I hit something for first time and also having a SendMessage to another script. So that message will be "Achived". And on the other script it wil be :
//JavaScript
var Achieved = false;
function Achieved(){
if(Achieved == true)
OnGui()
}
function OnGui(){
GUI.Box(Rect(1,1,Screen.widht,Screen.height),"Achievement Earned!")
if(GUI.Button(Rect(10,10,20,10),"Close"))
//Closes the GUI.Box
}
I dont know if this works or not I´m not good in sccripting just 13. Also to close the GUI.Box what do I use there? Help me please. And one thing - So when I hit something the script will send a message to the other script and the GUI.Box should pop-up saying that we Earned the achievement and if I click the button the Box should close.
ST
Answer by aldonaletto · Aug 19, 2012 at 02:53 AM
You don't call OnGUI: all On... functions are events called by Unity when the specified action happens (like OnTriggerEnter, for instance). You should instead use a boolean variable to control whether the GUI items will appear or not - like this:
private var achieved = false; private var showGui = false;
function Achieved(){ if (achieved == false){ // if not achieved yet... achieved = true; // set flag achieved to true... showGui = true; // and show GUI items } }
function OnGUI(){ if (showGui){ // if GUI enabled by showGui: // draw the controls: GUI.Box(Rect(1,1,Screen.widht,Screen.height),"Achievement Earned!") if(GUI.Button(Rect(10,10,20,10),"Close")){ showGui = false; // set showGui to false to hide these GUI items } } }
So Basicaly when the player trigger something in his scripts like - OnCollsion or if he Jumped 10000 times. Then it sends a message to this script that you've posted. Then it should work exact as you said.
Yes: you must call the function Achieved via Send$$anonymous$$essage, what will set the variables achieved and showGui - the first shows that Achieved was called, and the latter enables the GUI items. An example: the script below can be attached to a trigger object, and will call the function Achieved in any player script when the player enters the trigger
function OnTriggerEnter(other: Collider){ if (other.tag == "Player"){ // remember to tag the player as "Player" other.Send$$anonymous$$essage("Achieved"); } }NOTE: $$anonymous$$y answer had some errors: variable and function with the same name (Achieved) and OnGUI written as OnGui . It's fixed now.
Your answer
Follow this Question
Related Questions
How to set high score (best time) with playerprefs 1 Answer
GUI box problem? 0 Answers
Stand alone player 0 Answers
Question on playerprefs 2 Answers
GUITime and Score issue. 0 Answers