Destroy object with trigger and add value to variable

So the player character and the npcs in this game have the same need system. For example hunger is what I am working on right now. I know how to make the number drain to zero (or more) but what I need to do next is destroy a game object (eat it) and increase the value by a certain amount. The player needs to press e near an object, but NPCs should be able to do it themselves

#pragma strict

	public var fullness = 100;
	public var fatigue = 100;
	public var healthPoints = 10;
	public var drainRate = 5;
	
	
	
function eat(){
	if(Input.GetKey("e")){
		fullness = fullness + 5;
		Destroy(rat);
		
	}
	
}
function hunger() {
	fullness = fullness - drainRate;
	
}
function Start () {
	InvokeRepeating("hunger",.01, 5.0);
	
}

function Update () {
	
}

Hello there , I am new to trying to answer questions :slight_smile: If I understood well , you want to destroy the game object called rat … this cannot be done unless you “declare” rat. for example you declare an int x or a string y , in this case you need to declare a gameobject rat then you can type destroy(rat).

however for the purpose you are trying to reach i think it might be better to write the following:

function OnTriggerEnter (other : Collider) 
{
   if(other.tag=="rat")
   {
     // remove eat function
     if(Input.GetKey("e")){
         fullness = fullness + 5;
     Destroy(other.gameObject);
   }
}

and don’t forget to add a tag rat to your rat object in the scene and add a collider and check the trigger box