- Home /
Question by
Mr_SpoderMan · Jul 29, 2021 at 05:39 PM ·
unity 2dprogrammingreferencing
Object I re-enable cannot be referenced
After I re-enable a gameobject, any script which tries to reference it tries to do it but fails. Every script references the gameobject after it has been enabled. I really don't understand the problem and would really enjoy some help. this is the script I am using for reference:
public float coolDown = 1;
public float coolDownTimer;
public GameObject player;
void Update()
{
if (coolDownTimer > 0)
coolDownTimer -= Time.deltaTime;
if (coolDownTimer < 0)
coolDownTimer = 0;
player = GameObject.FindGameObjectWithTag("Player");
}
void OnTriggerEnter2D(Collider2D hitInfo)
{
if (hitInfo != null && (hitInfo.CompareTag("Player")) && coolDownTimer == 0 && player.GetComponent<Dashing>().coolDownTimer == 0)
{
StartCoroutine(RespawnPlayer());
}
}
void OnTriggerStay2D(Collider2D hitInfo)
{
if (hitInfo != null && (hitInfo.CompareTag("Player")) && coolDownTimer == 0 && player.GetComponent<Dashing>().coolDownTimer == 0)
{
StartCoroutine(RespawnPlayer());
}
}
public IEnumerator RespawnPlayer()
{
GameObject player = GameObject.FindGameObjectWithTag("Player");
player.SetActive(false);
player.transform.GetChild(0).gameObject.SetActive(false);
yield return new WaitForSeconds(2);
player.SetActive(true);
player.GetComponent<PHealth>().PlayerTakeDmg(1);
player.transform.position = new Vector2(0, 0);
player.GetComponent<PHealth>().coolDownTimer = 1;
coolDownTimer = coolDown;
StartCoroutine(player.GetComponent<PHealth>().FlashCo());
}
After the object is disabled and after that re-enabled the Collider2D hitInfo is not found by the script while collision happens.
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to move two game objects on a limited area of the screen? 0 Answers
Understanding Profiler and Optimization ! 0 Answers
My script does not find class form another script. 3 Answers
Raycasthit2d isnt working. 2 Answers