- Home /
how to call a method from another script using trigger?
hey! i have this playerHealth script where everything from setting up the UI, healthvalues etc. are put in, it also has a heal method where when a player collides with a game object that has the tag healthOrb it would increase the healthvalue by 1
public class playerHealth : MonoBehaviour
{
public int maxHealth = 5;
public int currentHealth;
public healthBarController healthbar;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthbar.SetMaxHealth(maxHealth);
}
public void OnCollisionEnter2D (Collision2D other)
{
if(other.collider.tag == "monster")
{
TakeDamage(1);
}
}
public void healHealth(int Heal)
{
currentHealth += Heal;
healthbar.SetHealth(currentHealth);
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthbar.SetHealth(currentHealth);
}
}
the only concern i have with this is refrencing the healhealth method in another script and calling it through trigger
public class trigger : MonoBehaviour
{
public playerHealth playerhealth;
public GameObject breakParticle;
public GameObject healthparticle;
public cameraShake Shake;
void start()
{
Shake = GameObject.FindGameObjectWithTag("screenShake").GetComponent<cameraShake>();
}
public void OnTriggerEnter2D (Collider2D other)
{
if(other.GetComponent<Collider2D>().tag == "player")
{
Shake.camshake();
Destroy (gameObject);
Instantiate (breakParticle, transform.position, Quaternion.identity);
}
if(other.GetComponent<Collider2D>().tag == "monster")
{
Shake.camshake();
Destroy (gameObject);
Instantiate (breakParticle, transform.position, Quaternion.identity);
}
if(other.GetComponent<Collider2D>().tag == "healthOrb")
{
//this doesnt work btw, i think because it didnt register even when no error was shown on the
console
playerhealth.healHealth(1);
Destroy (gameObject);
Instantiate (healthparticle, transform.position, Quaternion.identity);
}
}
}
sorry if my script looks sht im stll learning as of now(・-・;)
the playerhealth is attached to the player and the trigger script is attached to the gameobject (healthOrb) with trigger on that will trigger the particle and heal method
Answer by kylecat6330 · Jan 27 at 09:09 AM
Just some general things I saw that may help overall. First of all you shouldn't use other.GetComponent().tag on all your if statements you should be able to just do other.tag instead because other is already the Collider2D. If for whatever reason you can't use other.tag simply set it to a variable at the top like "string tag = other.GetComponent().tag" and then use tag in your if statements.
Now as for your problem it may be better to use a "playerHealth = other.GetComponent()" in OnTriggerEnter under your health tag rather than declare it as a public. That will make sure that they a referencing the correct scripts which may be your problem. If that doesn't fix then add some Debug.Logs to your script to see if the functions are being called but just are not working, or if they are not being called.
thank you very much! i will surely try this right now and also as for the if statement other.Getcomponent().tag i didnt actually write that down what i wrote was just a simple other.collider.tag but after the API update it change my script to other.GetComponent().tag
Your answer
Follow this Question
Related Questions
LoadLevel Health Question. 1 Answer
Health does not update in this script, why? 1 Answer
Can't click gameobject when over another trigger? 1 Answer
Health System Help 2 Answers