- Home /
GameObject Instantiate and check if "activeInHierarchy" (HELP)
Hello everyone! In my code below, I am trying to check if "enemy" is active in the scene. "enemy" is a gameObject variable that equals the instantiate code, but nothing happens when I check if it is active in the scene or not, or even if it is null. In my game, I have enemy tanks spawning from all sides of the screen. They spawn in waves in this "for loop." But once the last enemy of the loop is null (not in the scene or dead), I have the next round text pop up. Anyways, the top of my description is what I need help with. I just wanted to give you guys a broad background on what's going on in my game. Thanks in advance!
CODE:
for (int enemySpawnMark = 0; _enemyCount > enemySpawnMark; enemySpawnMark++) { float spawnY = Random.Range(0, 7); float spawnX = Random.Range(0, 12.4f);
Vector3 leftScreen = new Vector3(-13.5f, spawnY, 0);
Vector3 rightScreen = new Vector3(13.5f, spawnY, 0);
Vector3 topScreen = new Vector3(spawnX, 8, 0);
Vector3 bottomScreen = new Vector3(spawnX, -9, 0);
Vector3[] spawnPosValues = new Vector3[4];
spawnPosValues[0] = leftScreen;
spawnPosValues[1] = rightScreen;
spawnPosValues[2] = topScreen;
spawnPosValues[3] = bottomScreen;
Vector3 spawnPos = spawnPosValues[Random.Range(0, spawnPosValues.Length)];
Quaternion spawnRot = Quaternion.identity;
if (enemySpawnMark == _enemyCount - 1)
{
GameObject enemy = Instantiate(_enemyPrefab, spawnPos, spawnRot) as GameObject;
if (enemy.activeInHierarchy == false)
{
GameObject canvas = GameObject.FindWithTag("Canvas");
_UIManager uiManager = canvas.GetComponent<_UIManager>();
if (uiManager != null)
{
uiManager.SetRoundText();
}
}
}
else
{
Instantiate(_enemyPrefab, spawnPos, spawnRot);
}
yield return new WaitForSeconds(_spawnDelay);
}
Your answer
Follow this Question
Related Questions
Setting a variable to an instantiated object. 1 Answer
Setting the variable on a GameObject to be instantiated (not yet existent) cs and js 2 Answers
How to stop a prefab from changing a variable 0 Answers
Lock rotation of an instantiated object to another 1 Answer
Have a Timer Counts To 5 sec. Then Make a Sphere Appear While Using Instantiate 1 Answer