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 KemoM · Jun 23, 2015 at 03:07 PM · 2dtimertutorial

Extend time by picking up object problem

I am playing with Unity's Catch Game tutorial, and trying to add an object that would extend time when picked up. I added a timer like this in fixed update:

 void FixedUpdate(){
     if (playing) {
         timeLeft -= Time.deltaTime;
         if (timeLeft < 0) {
             timeLeft = 0;
         }
         UpdateText ();    
     }

 }

and then i did this:

 void OnCollisionEnter2D(Collision2D collision){
     if (collision.gameObject.tag == "timer") {
         timeLeft += 10;
     }
  }

I also tried with void Update(), but id did not work. Thanks in advance.

Comment
Add comment · Show 2
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 Scribe · Jun 23, 2015 at 07:31 PM 0
Share

Is your timer collider set to isTrigger = true by any chance, that would be the norm for pickups and such, in which case you would need to use OnTriggerEnter2D.

avatar image KemoM · Jun 23, 2015 at 09:53 PM 0
Share

It was not trigger, I've tried to make it trigger and used the OnTriggerEnter2D but it didn't work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Ibzy · Jun 23, 2015 at 03:23 PM

Main thing I see here is that you don't call UpdateText() after adding the time (though this still should show after the next FixedUpdate). Are you sure you are checking for the tag exactly? Is there a capital T maybe?

Try putting a Debug.Log("Time Added") after timeLeft += 10; to see if the collision tag is "timer", and a Debug.Log(collision.gameObject.tag); outside the if statement to check an actual collision happened.

Comment
Add comment · Show 17 · 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 KemoM · Jun 23, 2015 at 04:47 PM 0
Share

Thanks for the quick reply. I posted only small portion of the code, the update text is in the code:

void UpdateText(){ timerText.text = "Timer:\n" + $$anonymous$$athf.RoundToInt (timeLeft); }

It wasn't the tag, I copy-pasted it, and when I tried to log this and nothing happened, not even the collision :S

avatar image Ibzy · Jun 23, 2015 at 07:00 PM 0
Share

Sounds like the collision isn't happening then. Do you have a collider on both the timer object and the character (I imagine you do on the character for other drops to work) and a RigidBody on at least one (probably character again)?

avatar image KemoM · Jun 23, 2015 at 09:55 PM 0
Share

I have both collider and rigidbody (2d) on both objects. When I pick up other objects I get points, so this should work as well, but as you said there is something with collision.

avatar image Ibzy · Jun 23, 2015 at 10:02 PM 0
Share

Could you share a snip of the inspector on both objects with all components "expanded"? $$anonymous$$ight help us see where there might be an issue :)

avatar image Ibzy · Jun 24, 2015 at 03:06 PM 1
Share

That will be your issue then - the collision isn't happening because the gamecontroller isn't attached to the hat (as far as I can tell in the tutorial?) and thus has not collider. Simply attaching the script to the Hat should suffice, but I think the cleaner approach would be to add your check inside OnTriggerEnter2D() into the existing one in BowlController and then adjusting the timeLeft variable (most easily as a static variable)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Skipping over timer 1 Answer

Countdown Timer or WaitForSeconds? 2 Answers

Is the 2D gameplay tutorial by Graveck
 Interactive outdated? 1 Answer

Any 2D side scroller tutorials? 2 Answers

Question about 2D Gameplay Tutorial 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