Starting Coroutine from another script problem.
Hi, I'm having a problem trying to call an IEnumerator for a power up, I have been able to make my first power up script work and am using an almost exact copy of that code on this script but the co routine is not starting in the other script, any help would be appreciated, Thanks.
Power Up Script.
using UnityEngine;
using System.Collections;
public class PowerUp2 : MonoBehaviour {
GameObject powerUp2;
public AttackTrigger Buff;
public GameObject Heavy;
public GameObject Light;
void Start()
{
Buff = Heavy.GetComponent<AttackTrigger>();
Buff = Light.GetComponent<AttackTrigger>();
powerUp2 = GameObject.FindGameObjectWithTag("powerUp2");
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Player")
{
Buff.StartCoroutine("Buff2");
Destroy(this.gameObject);
}
}
}
## Script To Be Powered Up.
using UnityEngine;
using System.Collections;
public class AttackTrigger : MonoBehaviour {
[SerializeField] int damageVal;
[SerializeField] int critVal;
HealthBloodKnight critFill;
public GameObject player;
public bool buffed;
void Start() {
critFill = player.GetComponent<HealthBloodKnight>();
buffed = false;
}
void OnTriggerEnter(Collider col) {
if(col.tag == "Enemy") {
col.gameObject.GetComponent<HealthEnemy>().TakeDamage(damageVal);
if (!buffed && Random.value <= 0.2)
{
col.gameObject.GetComponent<HealthEnemy>().TakeDamage(critVal);
print("Regcrit");
critFill.Crit();
}
if (buffed && Random.value <= 0.9)
{
col.gameObject.GetComponent<HealthEnemy>().TakeDamage(critVal);
print("Bufcrit");
critFill.Crit();
}
}
}
public IEnumerator Buff2()
{
print("IamBuffed");
buffed = true;
yield return new WaitForSeconds(40f);
buffed = false;
yield return null;
}
}
You don't need to call StartCoroutine from AttackTrigger. The method comes from $$anonymous$$onoBehaviour that you inherrit. Also it is not a good practice to run something using strings. So try replacing Buff.StartCoroutine("Buff2"); with StartCoroutine(Buff.Buff2())
Also for future problems like this, try debugging with breakpoints or using Debug.Log() to see where it does not get called, so you know which line of code should be altered.
Okay got it working now, thanks for the advice.
Your answer
Follow this Question
Related Questions
Coroutine not working as expected 1 Answer
Randomized array result from other scipt 2 Answers
Persistent Timers in Couroutines 0 Answers
Issues with video player coroutine 2 Answers
Co-routine a recursion help 0 Answers