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 /
This question was closed May 09, 2019 at 02:54 PM by Jjeesee for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Jjeesee · May 08, 2019 at 01:07 PM · unity 5programmingtimercountdownpick up

I want to pick up objects to increase timer.

I have a timer that counts down every second (which I got from a tutorial), and when it reaches 0 it's Game Over. I want to be able to pick up objects (water) to increase the time, but don't know how.

 Image timerBar;
 public float maxTime = 5f;
 public float timeLeft;

 public float water = 1f;

 public GameManager theGameManager;

 void Start()
 {
     timerBar = GetComponent<Image>();
     timeLeft = maxTime;
 }

 void Update()
 {
     if(timeLeft > 0)
     {
         timeLeft -= Time.deltaTime;
         timerBar.fillAmount = timeLeft / maxTime;
     }
     else
     {
         Time.timeScale = 0;
         Destroy(gameObject);
         theGameManager.RestartGame();
     }
 }

 void OnCollisionEnter2D(Collision col)
 {
     if (col.gameObject.tag == "Water")
     {
         timeLeft += water;
     }
 }

}

Comment
Add comment · Show 4
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 xxmariofer · May 08, 2019 at 02:00 PM 0
Share

whats wrong with your code since it seems fine, i imagine that the collision is not getting fired, the collision is between sprites right? one of the objects have a rigidbody attached? if not add 1 to atleast one of them

avatar image Jjeesee xxmariofer · May 08, 2019 at 02:23 PM 0
Share

When the player runs into the water, nothing happens. The player has a rigidbody, and I tried adding it to the water but that does nothing. The water only has a circle collider2d and a script that destroys it when touched.

avatar image TreyH · May 08, 2019 at 02:41 PM 0
Share

Is this script attached to the same object (or a child of something) with the collider?

avatar image Jjeesee TreyH · May 09, 2019 at 10:59 AM 0
Share

Yes, it is.

2 Replies

  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · May 08, 2019 at 02:47 PM

Hello @Jjeesee

Your code seems right. If is not working is because OnCollisionEnter2D() is not beeing called.

-First, I'm supose game is 2D and both player and Water have a collider2D.

-This script must be placed in the player itself, that have a collider and a rigidbody. (This collider must not be selected as Trigger: Im not 100% sure for this)

-Water element must also have a collider2D (not trigger)

-Water object must be tagged as "Water" , not "water" nor "WATER"

-Be sure colliders are realyl colliding (As its a 2D collider, you need to be sure they are all in same plane)

And i think thats all you need to make it works... To be sure, as a good practice, aways check first if the method is beeing called, by using a Debug.Log, like this:

  void OnCollisionEnter2D(Collision col)
  {
      Debug.Log("OnCollisionEnter2D is called");
      if (col.gameObject.tag == "Water")
      {
          timeLeft += water;
      }
  }

Good luck!

Comment
Add comment · Show 4 · 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 Jjeesee · May 09, 2019 at 10:02 AM 0
Share

They both have a collider2d. I moved the code to the player, and now Debug works, but that's all I could get working. The tag is correct. I can destroy the water, and in the Inspector, time left increases with 1, but if trigger is removed nothing happens.

avatar image TreyH Jjeesee · May 09, 2019 at 11:07 AM 0
Share

What happens when you print the tag of that object?

avatar image Jjeesee TreyH · May 09, 2019 at 01:30 PM 0
Share

WATER IS TOUCHED UnityEngine.Debug:Log(Object) PlayerController:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/PlayerController.cs:153)

In PlayerController I've moved the script under ontriggerenter beacuse there it didn't work under oncollision.

Show more comments
avatar image
0

Answer by Jjeesee · May 09, 2019 at 02:54 PM

Ok, I solved it! I pulled the timer script into the playercontroller in inspector, I just had to refer it. The reference wasn't there from the beginning, but it got added once I moved part of the script to playercontroller, and then I just failed to refer it. I think that was all...

Sorry for the beginner ignorance? I appriciate all the help, thanks!!

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

Follow this Question

Answers Answers and Comments

208 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

Related Questions

Multiple Cars not working 1 Answer

Mini Game Timer - Unity 2017 0 Answers

how to show coordinates on the prefab? Using multidimensional arrays? 0 Answers

How to reset Time.time ? 1 Answer

Making a Timer Out out of 3D Text using C#. 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