I’m currently making a simple game for educational purposes and have stumbled across a problem. I’ve made a method called Kill() in a script called Player.cs and I now want to call this function from a script called KillTrigger.cs. So how would I do this? I’ve tried to make the method static but that wont let me call GetComponent. I’ve also tried to create another copy of Player.cs inside KillTrigger.cs by doing Player ply = new Player();. But that forced my to add GameObject.Find() infront of GetComponent, inside of Player.cs (Which I’ve heard takes long time to perform).
Player.cs
public void Kill()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0f, -6.5f);
Alive = false;
}
KillTrigger.cs
using UnityEngine;
using System.Collections;
public class KillTrigger : MonoBehaviour {
private void OnTriggerEnter2D(Collider2D collision)
{
//KillPlayerHere
}
}