- Home /
Can't start Coroutine because GameObject is "inactive"?
So iv'e created a simple power up system that woks fine when you first use the scene. But i have the problem that the coroutine doesn't start once the player dies and respawn's. The object still destroy's itself but doesn't apply the change in stats or the pickupEffect as i would like it to. the script is:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PowerUpExtraJump : MonoBehaviour {
public float multiplier = 2f;
public float duration = 4f;
public GameObject pickupEffect;
void Awake()
{
pickupEffect.SetActive(false);
GetComponent<SpriteRenderer>().enabled = true;
GetComponent<Collider2D>().enabled = true;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
StartEffects(other);
}
}
void StartEffects(Collider2D other)
{
StartCoroutine(Pickup(other));
pickupEffect.SetActive(true);
}
IEnumerator Pickup(Collider2D player)
{
Instantiate(pickupEffect, transform.position, transform.rotation);
PlayerController stats = player.GetComponent<PlayerController>();
stats.extraJumpsValue *= multiplier;
GetComponent<SpriteRenderer>().enabled = false;
GetComponent<Collider2D>().enabled = false;
yield return new WaitForSeconds(duration);
stats.extraJumpsValue /= multiplier;
Destroy(pickupEffect);
Destroy(gameObject);
}
}
I can't tell whats going wrong with the script because the error i'm getting is that the game object itself isn't active not the pickupEffect? The object still appears in the scene and in the inspector it is active but the Coroutine isn't? sorry if this doesn't make any sense i'm relatively new to Unity.
Thanks for your time.
Answer by Zymurer · Dec 15, 2018 at 05:10 PM
Maybe it is not working you destroy them. Try to define them as command lines if that doesn't work try to set theirs Mesh Renderer wrong instead of destroying them.
Could that help?
rend = GetComponent<Renderer>();
rend.enabled = true;
Sorry if that did not help.
Thank you for helping me out! checking wether or not it was being called helped me find the problem - it was in my player script when i detect the object through a tag to deter$$anonymous$$e the amount i have collected. In the player script i was destroying the object on trigger enter but i wanted to do that through my power up script ins$$anonymous$$d. Thank you!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Laser beam using particle system Unity2d. 0 Answers
Particle System not rendering at times 0 Answers