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
0
Question by daX-_-_908 · Jun 25, 2020 at 03:03 PM · scripting problemscripting beginner

How can I stop a stopwatch using "on trigger enter"?

Hi, I'm new to Unity and I'm still learnig. I made a little level where a ball has to reach the end before the timer reach the 0. When the player reach the end a win screen appear and show you the score you have made and your time to complete the level. What I want to know is: How can I stop the time in the winscreen after the player reached the end?

,Hi, I'm new to Unity and I'm still learnig. I made a little level where a ball has to reach the end before the timer reach the 0. When the player reach the end a win screen appear and show you the score you have made and your time to complete the level. What I want to know is: How can I stop the time in the winscreen after the player reached the end?

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
1

Answer by AaronBacon · Jun 25, 2020 at 03:30 PM

Would be helpful if you showed the scripts that handle the timer, but one simple way to do it would be to have a collider on the GameObject at the ending that is set to be a trigger, and also tag it (the preset "Finish" tag sounds like a good fit): TagExample

Then, on the Player, you just do a function like this:

 void StopFuction()
       {
             // put some code to stop the clock from counting up
            // could be just setting a boolean that the clock will check
       }
 
 void OnTriggerEnter(Collider coll) // "coll" will now refer to the object you collided with 
     {
         if(coll.CompareTag("Finish")) // Check if the collider is tagged "Finish"
         {
                     StopFunction();
         }
     }


Of course you can do this the other way round too, tagging the player as "Player" and instead have the Ending collider have a script on it that waits for a collision with something tagged "Player".

Like I said, hard to do without seeing your code, but that's how you could generally do it.

Comment
Add comment · Show 1 · 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 daX-_-_908 · Jun 26, 2020 at 10:19 AM 0
Share

Thanks for the good advices I've tried to write the code you've posted but it didn't work (I think because I miss a reference somewhere) So I'll try to share the script. The stopwatch script is literally made by copying and pasting the countdon script and I've changed the timeStart -= Time.deltaTime with the plus:

Stopwatch script:

 public class stopwatch : $$anonymous$$onoBehaviour
 {
     public float timeStart = 0;
     public Text$$anonymous$$eshProUGUI textBox;
     private bool isGameStarted = false;
     private bool gameEnded = false;
 
     // Start is called before the first frame update
     void Start()
     {
         textBox.text = timeStart.ToString();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if (gameEnded == false)
         {
             timeStart += Time.deltaTime;
             Debug.Log(timeStart);
 
             textBox.text = $$anonymous$$athf.Round(timeStart).ToString();
         }
         if (timeStart < 0)
         {
             timeStart = 0;
             isGameStarted = false;
             gameEnded = true;
 
         }
 
         if (gameEnded == true)
         {
             FindObjectOfType<Gamemanager>().EndGame();
             
 
         }
     }

End trigger script:

 public class Endtrigger : $$anonymous$$onoBehaviour
 {
     public  Gamemanager Gamemanager;
     public GameObject Scermatadicountdown;
 
 
 
 
 
     private void OnTriggerEnter(Collider other)
     {
         Scermatadicountdown.GetComponent<countdown>().enabled = false;
         Gamemanager.Completelevel();
     }
 }




avatar image
1

Answer by Darkmatter520 · Jun 25, 2020 at 03:36 PM

You can use trigger events like this:


    private void OnTriggerEnter(Collider other) {
        // check if the Collider is the Ball
        if (other.gameObject.name == "Ball, or whatever it is called in the editor") {
            // stop the timer
        }
    }
Let me know if you need more help!

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

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

238 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Trying to find the highest number than add it to itself. 2 Answers

When my player dies and i hit revive button i want my player back to the game but i m unable to do that please help. 2 Answers

How to code Scorpion Mechanic? 0 Answers

Creating one class or using polymorphism? 1 Answer

Object not responding to OnTriggerEnter() 2 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