So I have a prefab which is an item drop and a timer attached to the main camera. When the player collides with the item, I need the item to destroy then add time to the timer.

The itemdrop handling script looks like this:

   public LifeTimer LifeTimer;

   // Destroy sprite once player collides into it 
   void OnCollisionEnter2D(Collision2D col){
           LifeTimer.currentTime += 10;


The life timer is a reference to my script as follow which is attached to the main camera.

public class LifeTimer : MonoBehaviour

    // Once level initializes, start current time to starting time
    public float currentTime = 0f;
    public float startingTime = 10f;

    [SerializeField] Text lifeTimerText;

    void Start(){
        currentTime = startingTime;

    void Update(){

        // Decrement time per second every second
        currentTime -= 1*Time.deltaTime;
        // Set currentime to text on screen
        lifeTimerText.text = currentTime.ToString ("0");

        // Load Game Over Screen when time goes to 0
       if(currentTime <= 0){

I realized I can't attach the main camera to the life timer component of the item drop script because the item prefab is not instantiated yet. What would be the best way to do what I'm trying to do, which is to add time to the timer. The on collision and destroying aspect works perfectly.

Thank you for any guidance


Let's do it the other way ;) When you Instantiate ItemDrop, you can retrieve LifeTimer from the Camera and save it. For instance, right after the instantiate of your prefab, the method Start() of your object ItemDrop will be called by Unity. In this, you could easily get the Camera (via Camera.mainif it's the main camera of your game), and get the LifeTimer component that's attached to it (via a simple GetComponent()). When the OnCollisionEnter2D is called, everything is set up and ready for the ItemDrop to add updat LifeTimer.

As a side note and for the only sake of learning and optimizing I'd suggest you to consider the following suggestions:

  • Consider this line of your code: public LifeTimer LifeTimer;. Using the same case for a variable name and its type can be REALLY confusing. Like at first I thought that your LifeTime class was static, when it was just a regular class field. I'm not 100% fond of it but I'd suggest you to have a look at the C# coding convention (in there, every variable name is written in lower case).
  • I don't know what the game is about and how many type of itemdrop you could have or even the number of them, or what object is in charge of instantiate them, but it looks like the perfect playfield for design patterns: Observer. I'd suggest you to have a look at it to have a clean and solid architecture with good responsibility about what is calling what and when. More about the observer pattern here.
  • \$\begingroup\$ Oh and welcome this site @Safder! \$\endgroup\$ – lvictorino Oct 21 '19 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.