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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Abhi94 · Jun 17, 2015 at 03:21 AM · script.timetimertime.deltatimeaccess

Can anyone tell how can i add 2 seconds to my TIMER from another script ?

SO i have a script for maintaining timer , but i want to access it from another script and add 2 seconds whenever "if"( condition is true) is used

THIS IS TIMER SCRIPT

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Timer : MonoBehaviour {
     
     public float myTime = 30f;
     
     // Update is called once per frame
     void Update () {
 
 
     GetComponent<Text>().text =  (myTime -= Time.deltaTime).ToString("f0");
 
 
         if(myTime<=0) {
             Debug.Log("GAME OVER");
         }
         
     }
 }
 

Now i have tried accessing it from this script , but it seems to restart my timer rather than adding 2 seconds

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class onClick1 : MonoBehaviour {
 
     public Text ScoreText;
     private int Count = 10;
     private int wrongAnswer = -5;
     public bool hello;
     **private float RightAnswer = 2.0f;**
 
 
     public    void SayHello(  bool hello = false ) {
 
 
         
       if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
 
             GameObject thePlayer = GameObject.FindGameObjectWithTag("GameController");
             DisplayColor displayColor = thePlayer.GetComponentInChildren <DisplayColor>();
 
             ChangeColor[] changeColors = thePlayer.GetComponentsInChildren <ChangeColor>();
             
             foreach(ChangeColor changeColor in changeColors ) {
                 Debug.Log(changeColor .gameObject.name);
                 changeColor.ColorME();
             }
 
             **Timer timer = thePlayer.GetComponentInChildren<Timer>();**
 
             
             hello = true;
             displayColor.changeColor();
             **timer.myTime =+ RightAnswer;**
             ScoreText.text = "SCORE: " + Count;
 
         
         
         
         } else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
     
             hello = false;
             ScoreText.text = "SCORE: " + wrongAnswer;
         
         }
         }
     
 }
 





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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Nerevar · Jun 17, 2015 at 07:23 AM

  **timer.myTime =+ RightAnswer;**

It should be "+=" and not "=+", "=+" is equivalent to "=".

Comment
Add comment · 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
0

Answer by HammerCar · Jun 17, 2015 at 10:04 AM

On line 35 there is

 **timer.myTime =+ RightAnswer;**

It should be "+=" not "=+".

Comment
Add comment · Show 7 · 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 Mouton · Jun 17, 2015 at 12:41 PM 0
Share

Same answer as Nerevar, less detailed.

avatar image Abhi94 · Jun 18, 2015 at 07:07 AM 0
Share

Thank you guys for helping me , but now my timer seems to go negative , so if you can help me with that.

avatar image HammerCar · Jun 18, 2015 at 08:58 AM 0
Share

It might be because in your timer script there is this.

 GetComponent<Text>().text =  (myTime -= Time.deltaTime).ToString("f0");

Here you are setting the myTime value so try to use only "-" insted of "-=".

avatar image Nerevar · Jun 19, 2015 at 07:16 AM 1
Share

You are actually doing a countdown? that is increased everytime there is a right answer? the game should end when your countdown reaches 0 is that it?

avatar image Abhi94 · Jun 20, 2015 at 04:52 AM 0
Share

Yes Nerevar i want my game to end and timer to stop after it reaches 0, but right now it seems to go negative

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Timer : $$anonymous$$onoBehaviour {
     
     public float myTime = 20f;
     
     // Update is called once per frame
     void Update () {
 
 
     GetComponent<Text>().text =  (myTime - Time.deltaTime).ToString("f0");
 
 
         if(myTime<=0) {
             Debug.Log("GA$$anonymous$$E OVER");
         }
         
     }
 }
 
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Time Manager Class Implementation Like Coroutine in Unity 1 Answer

Time does not start counting down when need to 1 Answer

CountDown Timer Help (Seconds problem) 2 Answers

time.deltaTime timer increasing faster and faster 1 Answer

Timer reset 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