Communication

I’m trying to communicate between two scripts. Well, that but not quite.

This is the first piece of code I’ll show:

function OnTriggerEnter(other : Collider)
{

	if(other.gameObject.tag=="Enemy")
	{

	var enemy1script : Enemy1Script = other.gameObject.GetComponent(Enemy1Script);
			
			if(enemy1script!=null)
			{
				enemy1script.Hit();
			}
			
		Destroy(gameObject);
		
	}
}

What this does is that every time an object tagged “Enemy”'s collider is triggered by another object, it locates a script on the “Enemy” gameObject, and finds a specific function.

Next:

function Hit(){

		
	var health : float = 100;	
		
	if(health!=0)
	{
		health -= 25;
	}
	else
	{
		Destroy(gameObject);
	}


}

This is the function which is located through using GetComponent.

This works to a point. When the game runs, the script is located, and health is shown in the debug log to be decreased to 75. Although, that’s as far as it goes, regardless of how many times the Trigger is triggered.

Any help?

Because you’re destroying the GameObject with the OnTriggerEnter code in it.