Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by BeeVee · Mar 26, 2013 at 11:03 AM · timerpickupadd

How to add time to a timer by picking up coins

Hi,

Me again with my maze game (you can try here: https://dl.dropbox.com/u/2807104/labU_webp/labU_webp.html and yes I know the jump makes it too easy). I want to severely restrict the timer, which is set at 10 minutes for the game now, so that you need to pick up coins, not just for the points but to give you more time to finish a level, but I'm beating my head against a wall to do so. I have two pertinent scripts, one called On_touch_2 is attached to the coins here:

 #pragma strict
 var score:GameObject;
 var barClock:barClock;
 
 function Start()
 {
     score = GameObject.Find("Score"); 
 }
 
 function Update () {
 
 }
 
 function OnTriggerEnter() {
     transform.parent.gameObject.audio.Play();
     Destroy(gameObject);
     ++scoreScript.score;
     ++barClock.AddTime;
 }

The other, attached to an empty GameObject is the timer, called barClock:

 #pragma strict
 
 function Awake () {
         DontDestroyOnLoad (this);
            clockFGMaxWidth = clockFG.width;
 
     }
 
 var clockIsPaused : boolean = false;
 static var startTime : float; //(in seconds)
 public var timeRemaining : float; //(in seconds)
 var percent : float; //for bar clock
 var clockBG : Texture2D; //for bar clock
 var clockFG : Texture2D; //for bar clock
 var clockFGMaxWidth : float;  //starting width of the FG bar for bar clock
 
 function Start()
 {
     startTime = 600.0; //time for the whole game
     timeRemaining = startTime;
 }
 function Update() {
     if (!clockIsPaused)
     {
         // make sure the timer is not paused
         DoCountdown();
     }
 }
 
 function DoCountdown() 
 {
     timeRemaining -= Time.deltaTime;
     percent = timeRemaining/startTime * 100; //for bar clock
     if (timeRemaining < 0)
     {
         timeRemaining = 0;
         clockIsPaused = true;
         TimeIsUp();
     }
     ShowTime();
 }
 
 function PauseClock()
 {
 clockIsPaused = true;
 }
 
 function UnpauseClock()
 {
 clockIsPaused = false;
 }
 
 public function AddTime() { //trying to add 5 seconds to the time left
     timeRemaining += 200;
 }
 
 function ShowTime() //set the variables and show the time onscreen
 {
     var minutes : int;
     var seconds : int;
     var timeStr : String;
     minutes = timeRemaining/60;
     seconds = timeRemaining % 60;
     timeStr = minutes.ToString() + ":";
     timeStr += seconds.ToString("D2");
     guiText.text = timeStr; //display time
 }
 
 function TimeIsUp()
 {
     Application.LoadLevel("Loser");
     Debug.Log("Time is up!");
 }
 
 function OnGUI() //don't forget to make the graphics GUI rather than texture!
 {
     var newBarWidth:float = (percent/100) * clockFGMaxWidth; // this is the width that the foreground bar should be
     var gap:int = 20; // a spacing variable to help us position the clock
     GUI.BeginGroup (new Rect(Screen.width - clockBG.width - gap, gap, clockBG.width, clockBG.height));
         GUI.DrawTexture (Rect (0,0, clockBG.width, clockBG.height), clockBG);
         GUI.BeginGroup (new Rect (5, 6, newBarWidth, clockFG.height));
             GUI.DrawTexture (Rect (0,0, clockFG.width, clockFG.height), clockFG);
         GUI.EndGroup ();
     GUI.EndGroup ();
 }

There's stuff that doesn't work in both scripts in my attempts to find a solution. The main problem being the On_touch_2 script with the following error:

 Assets/labU/Scripts/On_touch2.js(18,20): BCE0049: Expression 'self.barClock.AddTime' cannot be assigned to.

I figure the error means that Unity is trying to applying an AddTime function from the coin itself, but nothing I do seems to fix the issue (using GetComponent and Find mainly)

Any advice?

B

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
1
Best Answer

Answer by Maulik2208 · Mar 26, 2013 at 11:37 AM

 function OnTriggerEnter() 
 {
     transform.parent.gameObject.audio.Play();
     Destroy(gameObject);
     ++scoreScript.score;
      timeRemaining += 200;
     ++barClock.AddTime;
 }
 

i think you should try to add time just after the destroy of the coin and after updating score.....have you tried like this???? ``if yes then whats happen???? and one more thing by this you will not need the function ADDtime so comment it and check.....however this code is untested by me but you can feel free to try it and inform if you are having the same issue.... Cheers

Don't forget to mark the answer if found useful

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 BeeVee · Mar 26, 2013 at 12:12 PM 0
Share

The timeRemaining variable is not defined in the On_touch_2 script so I get an unknown identifier error. I wonder if it would be best to put everything in one script so I don't have problems like this?

B

avatar image BeeVee · Mar 26, 2013 at 12:25 PM 0
Share

But you sorted it $$anonymous$$aulik2208! Ins$$anonymous$$d of:

 timeRemaining += 200;

I put:

 barClock.timeRemaining += 200;

and it works! Yay!

avatar image Maulik2208 · Mar 26, 2013 at 12:43 PM 0
Share

Have a Happy Coding :)

avatar image subhankhalid402 · May 31, 2014 at 08:07 PM 0
Share

barClock is not a valid type :(

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Speed Affect Timer 1 Answer

Pick up and Time delay problems 1 Answer

How to Format Timer Counting Up from Zero? 0 Answers

Adding time to timer with pickup 1 Answer

How can I let users add numbers, but in time/hours/minutes?,When button pressed, add X amount of time. 1 Answer


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