Creating & accessing a function from a diffrent "OnTriggerEnter" Function

Hey guys, so i am having allot of trouble with this.

I had a function on my “player” that whenever it collided with a enemy, health would be taken off, it worked fine untill my player remained still and the enemys ran into him, in which nothing would happen.
Thanks to you guys someone pointed out that it was because the “enemy” was colliding with the “player”, not the other way around.

so the solution would be to create a function on the “enemy” that activated the function on my main script (attached to my player) so that the important code can be executed and my character can loose health.

does anyone know the basics? i have looked up heaps of answers about this but i unfortunately still cant get it to work.

does anyone know the code to access another function in another code and thence forth activating that function?
how would i write the function in the “main code” after i have successfully gotten my “enemy code” to access it?

Dave…

Hey Dave. So we meet again :slight_smile:

What you could do is something like this:

In the Enemy-Script:

function OnTriggerEnter(collision : Collider){  
  if(collision.gameObject.tag == ("Player")){ 
    collision.gameobject.SendMessage("CauseDamage");
  }
}

The method SendMessage calls a function in the script of another object. Here is a link to the script reference: Unity - Scripting API: GameObject.SendMessage

and in the Player-Script you posted in the other question you add this:

function CauseDamage(){
  BurgersGui.Burgers +=BurgerNegative;
  transform.localScale += Vector3(-0.1,-0.1,-0.1);
}

I hope this works for you and if not just ask.