Spawning multiple objects and setting active to true
I'm trying to spawn a bunch of enemies and increase the amount they spawn in a given time but am running into a problem where when 2 are spawned close enough to each other and touching, one of them is set to inactive and stuck there.
I have used an while loop to keep checking but doesn't seem to work either
here is my code for what I am trying to accomplish
public GameObject enemy;
public Transform spawLoc;
public float repeatRate;
public Text diplayScore;
public GameObject player;
public Transform leftPt;
public Transform rightPt;
public Button restart;
public float downForce;
private int score;
void Start () {
restart.gameObject.SetActive(false);
InvokeRepeating("RandEnemy", 2, repeatRate);
score = 0;
diplayScore.text = score.ToString();
InvokeRepeating("ScoreUpdate", 4.6f, repeatRate);
InvokeRepeating("reInvoke", 5, 5);
}
void reInvoke()
{
CancelInvoke("RandEnemy");
repeatRate = repeatRate / 2f;
InvokeRepeating("RandEnemy", 0, repeatRate);
}
void RandEnemy()
{
float randX = Random.Range(0f, 180f);
float randY = Random.Range(0f, 180f);
float randZ = Random.Range(0f, 180f);
float randPosX = Random.Range(-5.5f, 5.5f);
float randPosy = Random.Range(18f, 22f);
GameObject enemyInstance = Instantiate(enemy, new Vector3(randPosX, randPosy, 0f), Quaternion.Euler(new Vector3(randX, randY, randZ)));
enemyInstance.GetComponent<Rigidbody>().AddForce(new Vector3(0, -downForce * 100, 0));
while (!enemyInstance.activeSelf)
enemyInstance.gameObject.SetActive(true);
}
Comment
Your answer
Follow this Question
Related Questions
how do i check if a gameobject is not active? 0 Answers
Parent is not active...but there is no parent! 0 Answers
Client Spawning Player Objects 0 Answers
My checker work 2 times 0 Answers
Cannot Spawn networkbehaviour gameobject 0 Answers