Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by prabha1994 · Oct 01, 2015 at 12:04 PM · c#javascripttexture2dtimer

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);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);
       }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image prabha1994 · Oct 01, 2015 at 08:13 PM 0
Share

Please help me! @Cherno,I get the following error!

UnassignedReferenceException: The variable timerBar of GameController has not been assigned.

avatar image Cherno prabha1994 · Oct 01, 2015 at 08:16 PM 0
Share

Well, you need to drag a texture into the timerBar field in the inspector, of course. Use a simple monocolor rectangle, if you like.

avatar image prabha1994 Cherno · Oct 05, 2015 at 05:08 AM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges