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 potatoofmemphis · Dec 29, 2016 at 09:11 AM · c#clock

How to have a clock ticking down when my player collides with something

Hi so I only know the basics about scripting but I need to make a clock for my game. I have looked around a bit and nothing really fits my need or im just stupid and dont understand it. So if you could explain this too me like im 5, but once my player collides with something I need to have some text show up and start counting down from 30. While I have a basic idea of what it would look like (something like the code I have at the bottom?) I am not sure how to do the clock component and tune it to my needs.

 void OnTriggerEnter (Collider other)
 {
 if(gameObject.CompareTag("Pickup")
 {
 //start clock here?
 }


Allow me to elaborate a bit more; When my gameobject(the player) collides with an object with the tag Pickup I want the script to do 3 things, Switch Cameras (which I havent gotten to work but this is my code below ↓, maybe someone could help me fix it, What happens is that well nothing happens. The camera1 doesnt enabled and camera2 doesnt disable. Here is a vid displaying it: https://www.youtube.com/watch?v=x6EcGPt71qg&feature=youtu.be )

 using UnityEngine;
 
 public class idk : MonoBehaviour
 {
     public Camera camera1;
     public Camera camera2;
 
 
     void OnTriggerEnter(Collider other)
     {
 
         if (gameObject.CompareTag("Pickup"))
         {
             camera1.enabled = true;
             camera2.enabled = false;
         }
     }
 }
 

And then I want the countdown timer to begin starting at 30 seconds. And When it hits 0 I would like the scene to change(I dont really know how to do that. I know the code for switching scenes but im not sure how to implement it here.)

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
0

Answer by bokchoy4221 · Dec 29, 2016 at 09:37 AM

i think it would be something like this. You would need to assign the countDownText Text to UI Text object in your game.

 public int countDown = 30;
 public Text countDownText;

 void OnTriggerEnter(Collider other)
 {

   if (gameObject.CompareTag("Pickup")
         {
             countdown -= time.deltatime;
             countDownText.text = " " + countdown;
         }
 }
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 potatoofmemphis · Dec 29, 2016 at 09:59 AM 0
Share

I get a ton of errors when using your script. But 2 pro$$anonymous$$ent ones are countdown is a type but is used like a variable, namespace text could not be found and countdown is not valid in the given context

avatar image
0

Answer by UnityCoach · Dec 29, 2016 at 10:39 AM

You need to add :

 using UnityEngine.UI;

at the beginning of your script, as Text is part of it.

Also, you should change countDown to type float, it'll never go down otherwise, unless your framerate is so slow that deltaTime is greater than 0.5.

 public float countDown = 30.0f;

One more thing, gameObject refers to the GameObject you put that script on. You may want to change this to:

 if (other.gameObject.CompareTag("Player")

So that only the Player triggers the collection of the item.

Comment
Add comment · Show 13 · 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 potatoofmemphis · Dec 29, 2016 at 11:20 AM 0
Share

I used all your suggestions except the last one since im putting the script on a player than when colliding with a object with the tag Pickup the countdown timer begins.

But what happened was that once the player collides with the Pickup object nothing happens. I wanted the text to update and show that its counting down, i forgot to put this into the first question. Also I only want it to show once the gameobject collides with the pickup.

avatar image UnityCoach potatoofmemphis · Dec 29, 2016 at 12:29 PM 0
Share

You'll have to use

 if (other.gameObject.CompareTag("Pickup"))
     {
     }


gameObject otherwise refers to the gameObject the component sits on. Also, note that, as opposed to OnCollisionEnter(), the OnTriggerEnter message is only sent to the Trigger Collider. So, in this configuration, your Player must have a Collider set as "Trigger" and the Pickup must have a standard Collider. I usually do this the other way around, so that my Player can still collide with other objects with real collisions. $$anonymous$$y two cents.

avatar image potatoofmemphis UnityCoach · Dec 29, 2016 at 04:55 PM 0
Share

Yeah I had that in my code already, all I need help with is getting the text to update and count down from 30

Show more comments
avatar image potatoofmemphis · Dec 29, 2016 at 05:22 PM 0
Share

Which one has more benefits coruntine or boolean?

avatar image UnityCoach potatoofmemphis · Dec 29, 2016 at 05:37 PM 0
Share

If you want a "tick" every second for no longer than 30 seconds, I'd say go for the Coroutine. Performance wise, putting an if statement in Update probably has the same cost.

The best option would be to have a Component that does just that, then you don't put an if statement in Update, you simply turn it 'Off' on Awake, 'On' on OnTriggerEnter, and 'Off' again at the end of the countdown.

Another question comes to my $$anonymous$$d ; will the player be able to pickup several items, and what happens with the countdown then? If so, the Coroutine will have to be stopped/restarted, while the Update may just reset the countdown value.

avatar image potatoofmemphis UnityCoach · Dec 29, 2016 at 05:45 PM 0
Share

No the player will not be able to pickup several items, but I have to go know so I will respond to your answers later on when I get the chance.

Show more comments
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

289 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 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

how can i make device clock system using datetime and make life system? 0 Answers

How do I make a clock like in FNAF in Unity5? 1 Answer

HH:MM:SS game clock with modifiable start time error 1 Answer

Text Colour not changing? 2 Answers

Enemy attack radius 4 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