Question by
SparksCool · Nov 21, 2018 at 04:32 PM ·
beginnerweaponcombat
how do i make a function called from another script repeat?
im trying to make a combat system and when the player enters a trigger he loses HP and i want it to use ranged weapons so i want the script to repeat the hurt player function every 3 seconds long as the player is inside the trigger
Comment
this is what my script looks like: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HurtPlayer : $$anonymous$$onoBehaviour {
public int damageToGive;
public static PlayerHP intstance;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter2D(Collider2D other)
{
PlayerHP.InvokeRepeating("HurtPlayer", 0f, 3f);
{
if (other.gameObject.name == "Player")
{
other.gameObject.GetComponent<PlayerHP>().HurtPlayer(damageToGive);
}
}
}
private void OnTriggerExit2D(Collider2D other)
{
PlayerHP.CancelInvoke();
}
}
Best Answer
Answer by PhonArt · Nov 21, 2018 at 07:47 PM
When the player enter call this :
Script.InvokeRepeating("Function name", 0f, 3f)
And when he goes out, call this one :
Script.CancelInvoke();