- Home /
Add more seconds to the timer when colliding with an object.
So I am making a 2D game that has a countdown timer in every level. if you get to 0 seconds you lose and the level restarts. Now I want to make an object like a coin lets say that when you collide with it, you receive 5 more seconds.
Here is the script I am using for the timer:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class countdown : MonoBehaviour {
public string levelToLoad;
public float timer = 0f;
private Text timerSeconds;
// Use this for initialization
void Start () {
timerSeconds = GetComponent<Text> ();
}
// Update is called once per frame
void Update () {
timer -= Time.deltaTime;
timerSeconds.text = "Time left = " + timer.ToString ("f2");
if (timer <= 0.05) {
SceneManager.LoadScene (levelToLoad);
}
}
}
Answer by andrew-lukasik · Jul 24, 2017 at 11:51 AM
First of add this code to your countdown class:
public class countdown : MonoBehaviour
{
public float timer = 0f;
/// <summary>
/// This field will contain a reference to a countdown existing in the scene
/// </summary>
static countdown _instance;
void Awake ()
{
//register this countdown instance:
_instance = this;
}
public static void ModifyTimer ( float byAmount )
{
_instance.timer += byAmount;
}
}
Create ICollectible.cs file containing this:
/// <summary>
/// Interface ICollectible will distinguish collectible objects
/// </summary>
public interface ICollectible
{
//every class implementing ICollectible must have this method as public:
void OnCollected ();
}
Create CollectibleObject.cs and slap this component on all things collectible:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Collectible object
/// </summary>
[RequireComponent( typeof(Collider2D) )]
public class CollectibleObject : MonoBehaviour,ICollectible//<look it's implementing ICollectible
{
/// <summary>
/// Timer delta on collected
/// </summary>
public float timerDelta = 10f;
/// <summary>
/// Implementation code for intereface ICollectible's "OnCollected" method
/// </summary>
public void OnCollected ()
{
//modify timer:
countdown.ModifyTimer( timerDelta );
//disable this object:
gameObject.SetActive(false);
}
}
Player.cs, slap this on your player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Player component
/// </summary>
[RequireComponent( typeof(Rigidbody2D) )]
public class Player : MonoBehaviour
{
void OnCollisionEnter2D ( Collision2D coll )
{
//is it ICollectible:
ICollectible collectible = coll.gameObject.GetComponent<ICollectible>();
if( collectible!=null )
{
//collectible! ( ͡° ͜ʖ ͡°)
collectible.OnCollected();
}
{
//no collectible!
//(╯°□°)╯︵ ┻━┻
}
}
}
No need to make static countdown _instance;
because we just need $$anonymous$$odifyTimer
timer which is already static. he can do whatever in the OnCollected
and i think you should not provide this line gameObject.SetActive(false);
if he wants to destroy then write his on code.
public void OnCollected ()
{
//modify timer:
countdown.$$anonymous$$odifyTimer( timerDelta );
// do more what you want
}
I may be wrong but, in my $$anonymous$$d, making _instance static actually helps limit use of static to this single field. Because otherwise $$anonymous$$odifyTimer cannot access timer field which quickly leads to escalatory use of static (at least as commonly seen in new coders)
Answer by Oysterino · Jul 24, 2017 at 11:27 AM
try adding something like this:
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.CompareTag("Coin"))
{
timer += 5;
Destroy(collision.gameobject);
}
}
This should add 5 seconds to the timer and remove the Coin when colliding.
So all you Coin-like object need is a Collider and a tag that matches the current "Coin"-tag in the code above.
what I need is to register if the player collides with a coin. I don't think this will work because the timer can't collide with anything.
why timer will collide with anything? he is just asking that check Collision/trigger of player with the gameObject(Coin) and the simply add the timer value. You can add value to timer by just taking reference of the class or making timer value static and OnCollisionEnter
just invoke the event and in his listener add the value.