- Home /
Points system for unity
Hello, I am looking for advice for a points system. I have tried a couple things but they aren't working very well at all. First of all I am trying to create a planet system, when you hit a planet an audio clip and an animation plays. I have tried with two different scripts. Here are those scripts.
var PlayerScore : int;
var ScoreText = "Score: 0";
var MaxPoints : int;
function OnTriggerEnter(other : Collider){
if(other.tag == "Point"){
PlayerScore += 1;
ScoreText = "Score: " + PlayerScore;
Destroy(other.gameObject);
}
}
function Update(){
if(PlayerScore > MaxPoints){
PlayerScore = 0;
print("MaxReached");
ScoreText = "Score: 0";
}
GUI.Button(Rect(0,0,10,10),"Start");
}
this did not work. In this script my goal was that after the player got to 10 points while receiving 1 point each time they hit a planet a start button would appear. A score counter was supposed to appear in the corner of the screen. The script didn't do anything when I attached it to the planets, do I need to attach it to the player? If not if someone could help me with this that would be great. Thanks!
If you have 'Playerscore' in this script, you need to have just one instance of this script for it to be useful. So I'd say attach it to the player. If you attach this to every planet, then every planet will have a Playerscore that will increase by 1 when you hit the planet. You'd have to hit one planet 10 times to get its score to 10.
Also you're trying to constantly draw the start button in Update, not just when playerScore>$$anonymous$$axPoints. Also I believe drawing GUI.xxx elements only works inside OnGUI() method, not Update().
Answer by toddisarockstar · Feb 24, 2015 at 08:21 AM
NoseKill is right. i gave him a thumbs up. you have to use buttons in a separate function. make sure your planets have colliders and your player has collider and rigidbody cpmponents. assuming everything else in you script is working the adjustment would look like this :
var PlayerScore : int;
var ScoreText = "Score: 0";
var MaxPoints : int;
var gobutton=false;
function OnTriggerEnter(other : Collider){
if(other.tag == "Point"){
PlayerScore += 1;
ScoreText = "Score: " + PlayerScore;
Destroy(other.gameObject);
}
}
function Update(){
if(PlayerScore > MaxPoints){
gobuttons=true;
PlayerScore = 0;
print("MaxReached");
ScoreText = "i got"+PlayerScore;
}}
function OnGUI () {
if(gobuttons){
if(GUI.Button(Rect(0,0,10,10),"Start")){PlayerScore=0;gobutton=false;}
GUI.Label(Rect(Screen.width*0.5,Screen.height*0.5,20,20),ScoreText);}}
Sorry i didnt test it. let me know if it works now.
No, says there are a few compiler errors, once I debug it I will try again!
Okay I found the issue, you forgot the quotation marks in
if(gobuttons) {
It still is not working though. When ever I hit a planet it just lets me stay on the planet, ins$$anonymous$$d of destroying the planet gameObject and passing through. I just collide with the planet.
oh sorry you might have to say the destroy line like this:
Destroy (hit.collider.gameObject);
Answer by MjrDeathAdder · Feb 27, 2015 at 02:16 AM
GUI functions/commands are required to be in a void OnGUI(){ } function. This looks like your problem. This is an addition to toddisarockstar's answer.
i am all javascript. it would be good for me to learn a little C#, but i know there is no need for the term "void" in any javascript!
the "void" term is a C# sharp thing.
Your answer
Follow this Question
Related Questions
Having difficulties making a block be a death trigger for a ball. 2 Answers
I am trying to give my player points when an enemy dies 1 Answer
OnTriggerEnter not working in running game 4 Answers
How can I make my score counter increase incrementally and not jump from 0 to 100? 2 Answers
Double Score Problem ? 1 Answer