- Home /
C#: Q about creating Score system using GUItext
My game is a round maze where you need to roll the ball to the center. I need to create a score system ( i guess with gui text ) of how many times overall player hits the center with the ball. But i don't know what i am doing wrong in my code...
using UnityEngine;
using System.Collections;
public class Reset2 : MonoBehaviour {
private int Score = 0;
public GUIText MaxScore; //and yes i've dragged created gui text into empty field//
private GameObject[] Wally;
private Vector3 initialPosition;
private float distance = 0.5f;
void Start () {
initialPosition = transform.position;
}
void Update () {
transform.Translate(Vector3.zero * Time.deltaTime);
if( Vector3.Distance(transform.position, Vector3.zero) < distance)
{
transform.position = initialPosition;
Application.LoadLevel(Application.loadedLevel);
Score++; //i've tried "Score = 1;" too//
MaxScore.text=" " + Score; //Gui text is empty///
}
}
}
Answer by Mehul-Rughani · Sep 12, 2014 at 03:45 AM
Try This....Hope It Will Help You
using UnityEngine; using System.Collections;
public class Reset2 : MonoBehaviour {
public static int Score;
public GUIText MaxScore; //and yes i've dragged created gui text into empty field//
private GameObject[] Wally;
private Vector3 initialPosition;
private float distance = 0.5f;
void Start () {
initialPosition = transform.position;
}
void Update () {
MaxScore.text=" " + Score; //Gui text is empty///
transform.Translate(Vector3.zero * Time.deltaTime);
if( Vector3.Distance(transform.position, Vector3.zero) < distance)
{
transform.position = initialPosition;
Score++; //i've tried "Score = 1;" too//
Application.LoadLevel(Application.loadedLevel);
}
}
}
}
It seem to work, but i can't store "Score++;", because no matter where i put it, when the level reloads, it clear "Score" value
i guess it's becayse void doesn't return any value outside?
When You Load Your Level All The Variables are Reset to their original value Thats Why I Had Declare Score as a static Variable.
Your answer
Follow this Question
Related Questions
GUI Text Score counter 3 Answers
Animating Transitions for GUI Results 0 Answers
Currency score increase on pickup 1 Answer
How to make a Score GUI. 0 Answers
Why this Error when trying to add a score using the new GUI ? 0 Answers