I tried to create simple flappy bird game and everything works fine but when i wanted to show the Game Over text with fading (using the lerp and color.a) the “Coroutine couldn’t be started because the GameObject is inactive” error occured and I don’t know why… I tried the activeInHierarchy and it returned false but why? I repeated the process of creating the gameobject but it still did not work and the error occurs still ,here is some code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameSetup : MonoBehaviour {
private float Lerp = 0;
public Text gameover_text;
public GameObject player;
public GameObject ground;
void Start () {
Instantiate (player, new Vector3 (-12, 5, 0), Quaternion.identity);
Instantiate (ground, new Vector3 (0, -9, 0), Quaternion.identity);
Instantiate (ground, new Vector3 (0, 9, 0), Quaternion.identity);
}
public void GameOver()
{
StartCoroutine(Animate());
Debug.Log("Game Over");
Debug.Log("CurrentState: " + gameObject.activeInHierarchy);
}
private IEnumerator Animate()
{
Debug.Log("Coroutine started");
yield return new WaitForSeconds(0.5f);
Lerp += 0.1f;
Fader(Lerp);
if(gameover_text.color.a != 255)
{
StartCoroutine(Animate());
}
}
private void Fader(float lerp)
{
gameover_text.color = new Color(0, 0, 0, Mathf.Lerp(0,255,lerp));
Debug.Log("Current state: " + gameover_text.color.a);
}
}
Sorry I did not explain it enough… The script is on ‘GameScript_Handler’ object and that is inactive but when i start the game it generates the player and ground, the generate obstacle script works too (it is on the same object) only the coroutine don’t work
Code on Scripts can run, but coroutines can not (or other Unity methods, like Start, Update, etc…)
I would suggest trying by making the game object active. If for whatever reason you always want that object inactive, then you have the option to move the coroutine to some other script/game object… where it’s active
I want it to be always active … As I said, I tried to create the object again but still it doesn’t work… In editor it says it is active and activeSelf says it too but activeInHiearchy says it is inactive, why? Sorry guys I’m new at unity and I’m still learning it
I see… well, that means that some parent object is inactive. Which in turns means, for what matters in this topic, that your game object is also active.
You’d have to have whichever parent object(s) active, or as I mentioned before, move the coroutine to some other game object that is … entirely active.
If you’re still stuck on this, can you create a unity package (just ignore graphics/sounds) - just the “bare bones” of the game and post the package on the thread, I’d take a look at it for you…
Found the problem… Your lerp / fade or whatever code is a bit odd, too.
I will just tell you the issue, and you can ask about the fade or not, I leave it to you.
So, you were referencing the prefab as the game object with the coroutine. That’s why it was “inactive”.
I wrote a few snippets of code for you:
// in game setup
static GameSetup instance;
void Awake() {
instance = this;
}
public static GameSetup GetInstance()
{
return instance;
}
// in Player Collisions
// erased your old game object GS_ whatever it was called.
public GameSetup gameSetup;
void Start() {
gameSetup = GameSetup.GetInstance();
}
// this is your code, slightly modified because of my change.
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.otherCollider.tag == "ScoreGate")
{
Physics2D.IgnoreCollision(gameObject.GetComponent<Collider2D>(), collision.otherCollider);
}
else
{
/** I modified this; you could have kept it as a game object, but since you never used
it as a game object, and only used it as the type 'GameSetup', I thought this would be better.
**/
gameSetup.GameOver();
}
}
private IEnumerator Animate()
{
Debug.Log("Coroutine started");
yield return new WaitForSeconds(0.5f);
Color origCol = gameover_text.color;
// this will take ~ 1 second to complete / fade in.
for(float f = 0; f <= 1; f += Time.deltaTime) {
origCol.a = f;
gameover_text.color = origCol;
yield return null;
}
origCol.a = 1;
gameover_text.color = origCol;
}