- Home /
find in his own instance
I have a prefab who make alien spawn, and respawn if the alien is killed. Since I have several spawn prefabs, I have to wait that all Aliens had been killed to make them respawn. How could I do to make .Find() search only in his prefab?
Sorry I am new to unity and OOP so my question may be confused.
He is my script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class spawn : MonoBehaviour {
public GameObject alien;
private float respawnTimer = 3f, nextSpawn;
private bool alive = true;
void Start () {
Instantiate (alien, transform.position, Quaternion.identity);
}
void Update () {
if (alive == true && GameObject.Find("alien(Clone)") == null) {
nextSpawn = Time.time + respawnTimer;
alive = false;
}
if (alive == false && Time.time > nextSpawn) {
Instantiate (alien, transform.position, Quaternion.identity);
alive = true;
}
}
}
Comment
Ok I found how to solve my problem, I just kept a reference to the game object I instantiate then I tested if it was null or not:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class spawn : $$anonymous$$onoBehaviour {
public GameObject alien;
private float respawnTimer = 3f, nextSpawn;
private bool alive = true;
private GameObject clone;
void Start () {
clone = Instantiate (alien, transform.position, Quaternion.identity);
}
void Update () {
if (alive == true && clone == null) {
nextSpawn = Time.time + respawnTimer;
alive = false;
}
if (alive == false && Time.time > nextSpawn) {
clone = Instantiate (alien, transform.position, Quaternion.identity);
alive = true;
}
}
}