OnMouseDown on another object

Hi ! ^^

I don’t know how to do something with my script (change a value, creat a GameObject,…) when the mouse click on an object which not contains the script. How can I do this ?

I know the command OnMouseDown and Public GameObject and I think the solution will use this 2 commands

Here is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class Player : MonoBehaviour {
    
    	public float life;

        void Start ()

       {
               life = 3;
       }

    	void OnMouseDown ()
    
    // Here my code is uncomplete. I want the value ''life'' gains +1 when I click on a GameObject named ''food''//
    
    	{
    		life = life + 1;
    	}
    }

For the moment, the value ‘‘life’’ gains +1 when I click on the Player (which contains the script) but I want the value changes when I click on another GameObject named ‘‘food’’. Can you explain me how I do this ?

@akillingbeck, to get reference of an object, I use public gameObject

Can you post code you have now? There’s a couple of solutions for this. But it depends what you want. Do you just want to get a reference to the object you clicked on?

I added the code to my question

The easiest way to do this is to create a prefab of the “Food” and attach a small script to it and a collider:

     public class Food : MonoBehaviour {
     
         Player playerScript; 
 
         void Start ()
 
        {
               playerScript = GameObject.Find("Player").getComponent<Player>();
        }
 
         void OnMouseDown ()    
         {
             playerScript.life += 1;
             Destroy(gameObject);
         }
     }

Yeah, it works great ! ^^

A great thanks to you @SteenPetersen ^^