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 redrocket1 · Dec 13, 2015 at 04:33 PM · c#timer

Timer only goes for .02 seconds

Hi, I'm trying to make a timer that starts when a certain gameObject hits another gameObject. I have that part figured out, but I can't figure out why it only go for the split second (.02 seconds to be exact) that the gameObjects collide with each other. I want it so that let's say the timer is 15 seconds, so that it starts when the gameObjects collide and then continue even after one of the gameObjects are destroyed (because thats what happens when they collide). Any ideas? Let me know if anything isn't clear, Thanks!

Comment
Add comment · Show 9
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 acorrow · Dec 13, 2015 at 05:28 PM 0
Share

The fact that it's .02 exactly is telling... that is the default time step in the time settings I believe. Sounds like you are only ever executing 1 frame (or maybe this is just called on start and not update?)

You are going to have to post your code for help, but my guess is that you aren't actually working in the update loop? Or you have some other code to change/stop the time/timescale.

avatar image redrocket1 acorrow · Dec 14, 2015 at 02:15 AM 0
Share

alt text

screen-shot-2015-12-13-at-91120-pm.png (48.3 kB)
avatar image redrocket1 acorrow · Dec 14, 2015 at 02:16 AM 0
Share

alt text

screen-shot-2015-12-13-at-91142-pm.png (8.9 kB)
avatar image redrocket1 acorrow · Dec 14, 2015 at 02:17 AM 0
Share

alt text

screen-shot-2015-12-13-at-91142-pm.png (15.4 kB)
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by acorrow · Dec 14, 2015 at 03:14 AM

You want the countdown on the OTHER object that it collides with, not this one. Move all your timer code and what not over to a script on your other object, and create a PUBLIC void to trigger from your Powerup.

So in the power up script, on your Collision, just grab the script off the colliding object, and call a method in it...

     void OnTriggerEnter2D(Collider2D collider)
     {
         OtherObjectsScript otherThing = collider.GetComponent<OtherObjectsScript>();
         otherThing.MyCountdownStart();
     }
 
Comment
Add comment · Show 3 · 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 redrocket1 · Dec 14, 2015 at 03:33 AM 0
Share

Sorry, I'm not really getting what to do. I thought I did what you told me to do, and it's still only going for .02 seconds. $$anonymous$$aybe it's because I'm a beginner, maybe since its late at night here. I'll check it again tomorrow. Thanks for everything so far

avatar image redrocket1 · Dec 14, 2015 at 03:12 PM 0
Share

So yeah... I don't really know what to do. I've spent like a week on this. I think I'll just give up and maybe come back to it later. Thank you for all the help.

avatar image acorrow · Dec 14, 2015 at 03:27 PM 0
Share

It's really very simple. You are just thinking about it wrong:

You have 2 objects, a Player, and a Power Up. When the player collides with the power up, the power up needs to be destroyed, and the player needs to be effected someway, for a specific time period.

What you need, is 2 different game objects (Player and PickUp) each with a different Script (call them Player.cs and PickUp.cs). Give the player object a tag of "Player" and the pickup object a tag of "Pickup" (I prefer to use layers here, but this is basic on purpose).

You place your TI$$anonymous$$ER in Player.cs, and control it with a bool. Like this: Player.cs

 public class Player : $$anonymous$$onoBehaviour {
 
     public bool Hit { get; set; }
     public float timeRemaining = 15;
     public int Armor { get; set; }
     void Update()
     {
         if (Hit)
         {
             timeRemaining -= Time.deltaTime;
             //GUI Stuff
             if (timeRemaining <= 0)
             {
                 Hit = false;
                 timeRemaining = 15;
             }
         }
     }   
 }

Pickup.cs

 public class Pickup : $$anonymous$$onoBehaviour
 {
     OnTriggerEnter2D(Collider2D collider)
     {
         if (collider.CompareTag("Player"))
         {
             //Now that we know it was the player we collided with, 
             //get the script attached to the Player game object and do stuff with it
             Player player = collider.GetComponent<Player>();
             player.Hit = true; //Setting Hit (bool) to TRUE will cause the time to decrease in the update loop.
             player.Armor += 20; //Then you can access other parts of the Player script
         }
     }
 }

One object can use GetComponent (assu$$anonymous$$g the script you wrote is a monobehavior) to grab a script from another and interact with it.

Try this approach.

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

Perform action If Input is done in this time. 2 Answers

How do I stop my timer by accessing a variable from another script? 2 Answers

Editable Timer 0 Answers

Start and Stop Timer Help 1 Answer

A method running each millisecond in real time 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