Trying to create a freaking math clone. new learner and very low coding skills. Having trouble with setting the time limit for the process.Need Help?
I am trying to create a clone of the game "freaking math" using unity 3d for experimental purposes to learn game design. It involves simple math problems which must be solved within 2 seconds. I have generated the maths problems and validating the answers but I am just not able to proceed further in creating the time limit with a bar at the top.
The game controller file that i used in my game is as follows:
How should i add the time limit in to this. Just like the game "freaking math"
I need a shrinking time limit bar with a time limit of 2 seconds.
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class GameController : MonoBehaviour {
public Text MathText;
public Text ResultText;
public Text ScoreText;
public GameObject LosePanel;
public Text LosePanel_HighScoreText;
public Text LosePanel_ScoreText;
private int currentScore;
private int rightNumber;
private int leftNumber;
private int TrueResultNumber;
private int FalseResultNumber;
void Start()
{
currentScore = 0;
RandomMath ();
}
void RandomMath()
{
rightNumber = Random.Range (0, 10);
leftNumber = Random.Range (0, 10);
int mOperator = Random.Range (0, 2);
switch (mOperator) {
case 0:
TrueResultNumber=leftNumber+rightNumber;
MathText.text=leftNumber.ToString()+"+"+rightNumber.ToString();
FalseResultNumber=TrueResultNumber+Random.Range(-2,2);
ResultText.text="="+FalseResultNumber.ToString();
break;
case 1:
TrueResultNumber=leftNumber+rightNumber;
MathText.text=leftNumber.ToString()+"+"+rightNumber.ToString();
FalseResultNumber=TrueResultNumber+Random.Range(-2,2);
ResultText.text="="+FalseResultNumber.ToString();
break;
default:
break;
}
}
public void OnTrueButtonClick()
{
if (TrueResultNumber == FalseResultNumber) {
currentScore+=1;
ScoreText.text=currentScore.ToString();
RandomMath();
}
else {
LosePanel.SetActive(true);
LosePanel_ScoreText.text="Score: " + currentScore.ToString();
if(currentScore>PlayerPrefs.GetInt("HighScore"))
{
PlayerPrefs.SetInt("HighScore",currentScore);
}
LosePanel_HighScoreText.text="High Score: " + PlayerPrefs.GetInt("HighScore").ToString();
}
}
public void OnFalseButtonClick()
{
if (TrueResultNumber != FalseResultNumber) {
currentScore += 1;
ScoreText.text=currentScore.ToString();
RandomMath ();
} else {
LosePanel.SetActive(true);
LosePanel_ScoreText.text="Score: " + currentScore.ToString();
if(currentScore>PlayerPrefs.GetInt("HighScore"))
{
PlayerPrefs.SetInt("HighScore",currentScore);
}
LosePanel_HighScoreText.text="High Score: " + PlayerPrefs.GetInt("HighScore").ToString();;
}
}
public void OnPlayAgainButtonClick()
{
Application.LoadLevel (Application.loadedLevel);
}
}
Answer by Cherno · Oct 01, 2015 at 02:07 PM
I have never heard of "Freaking Math", but a timer and shrinking bar can easily be done with Time.deltaTime and GUI.DrawTexture.
public float timer_cur = 2f;
public float timer_max = 2f;
public bool runTimer = false;
public Texture2D timerBar;
void Start() {
runTimer = true;
}
void Update() {
if(runTimer == true) {
timer_cur -= Time.deltaTime;
if(timer_cur >= 0) {
timer_cur = timer_max
}
}
}
void OnGUI() {
if(runTimer == true) {
int pos_x = Screen.width / 2 - timerBar.width / 2;
int pos_y = 100;
int width = Mathf.RoundToInt(timer_cur / timer_max * timerBar.width);
GUI.DrawTexture(new Rect(pos_x, pos_y, width , timerBar.height), timerBar);
}
}
Please help me! @Cherno,I get the following error!
UnassignedReferenceException: The variable timerBar of GameController has not been assigned.
Well, you need to drag a texture into the timerBar field in the inspector, of course. Use a simple monocolor rectangle, if you like.
Can you please explain me clearly on how to create the monocolour rectangle. I tried creating an image, but i was not able to add it to timer bar field in the inspector. Couldnt figure out how to add the 2D texture. Please help me! @cherno
Your answer
Follow this Question
Related Questions
Convert a java script in a c#??? thank you 1 Answer
Start Finish Timer 0 Answers
Need help to put together 2 scripts 0 Answers
Click on object shows menu, but also move it. 0 Answers
Java Script to talk to C# script 0 Answers