- Home /
The question is answered, right answer was accepted
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;
}
}
}
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
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.
Is this script attached to the same object (or a child of something) with the collider?
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!
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.
What happens when you print the tag of that object?
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.
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!!
Follow this Question
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