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);
			}

	}
}

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.

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!
            //(╯°□°)╯︵ ┻━┻
        }
    }

}