- Home /
how do i use component gathered from ontriggerEnter, outside of that function?
trying to use the pHealth variable on a coroutine after the onTriggerEnter event, but pHealth wont work unless inside the trigger event,
private void OnTriggerStay (Collider other)
{
if (other.tag == "Player")
{
PlayerHealth pHealth = other.GetComponent<PlayerHealth>();
// i want to do this VVVV in the coroutine
pHealth.Health -= DamagePoints;
if (isCurrentlyAttacking == true && isCurrentlyDamaging == false)
{
StartCoroutine(DealingDamage());
}
}
}
Answer by MenimalsEntertainment · Jun 05, 2020 at 05:38 AM
I would just create a global variable at the top of the script and then assign it inside the function. That way you always have access to it, you just have to be careful to not use it before it's assigned (when it is still null).
Answer by Q-Ted · Jun 05, 2020 at 06:55 AM
Add a PlayerHealth argument to the DealingDamage function.
IEnumerator DealingDamage(PlayerHealth pHealth)
{
<your coroutine code>
}
Then pass 'pHealth' to the DealingDamage function like so
StartCoroutine(DealingDamage(pHealth));
Your answer
Follow this Question
Related Questions
How to disable a script on a bunch of instantiated objects with tag ? 1 Answer
C# | How to delay a method with parameters 2 Answers
Light problem 1 Answer
Powerup issue - Speed 1 Answer
Multiple Cars not working 1 Answer