spawning problems, using array for random objects,Spawning rand prefab from array, destroy when not in the view of camera, roll new one.
Soo... How to put it. I've spent 5 hours sitting on it for now (and some other things) and cannot find any clue. I hope there is somewhere a good soul that will help me somehow.
Setup: I have a player and a planet; the planet is covered with spawn points. The player is running around that planet (you know, like orbiting on it). The camera is placed behind the player and follows him.
There goes the problem: there are 3 scripts: isThisVisible, object_roller, destroyer.
isThisVisible and object_roller are placed on spawn points which NEVER got destroyed. destroyer is placed on a spawned object and as name reminds is just destroys an object whenever is outside of the camera view.
Basically everything works fine, objects are spawned in the start method and even disappear when not seen in camera view, but then it seems like every spawnpoint just looses his object_roller and only 1 spawnpoint spawns objects.
I really hope you understand me. If some1 was that into my problem i could rec a vid and upload on yt to more clear explanation.
Here goes the code:
public class object_roller : MonoBehaviour
{
public GameObject[] objectPrefabs;
public static bool objSpawned = false;
int index;
public static bool onOff = true;
private void Start()
{
index = Random.Range(0, objectPrefabs.Length);
Instantiate(objectPrefabs[index], transform.position, transform.rotation);
objSpawned = true;
}
private void Update()
{
if (onOff)
{
if (objSpawned == false)
{
index = Random.Range(0, objectPrefabs.Length);
Instantiate(objectPrefabs[index], transform.position, transform.rotation);
objSpawned = true;
}
}
}
}
public class IsThisVisible : MonoBehaviour
{
public static bool isVisible;
Renderer rend;
private void Start()
{
rend = GetComponent<Renderer>();
StartCoroutine(visibility());
}
public IEnumerator visibility()
{
if (rend.IsVisibleFrom(Camera.main))
{
Debug.Log("#isThisVisible:isVisible: True");
isVisible = true;
yield return new WaitForSeconds(0.5f);
} else
{
Debug.Log("#isThisVisible:isVisible: False");
isVisible = false;
yield return new WaitForSeconds(0.5f);
}
}
}
public class destroyer : MonoBehaviour
{
Renderer rend;
private void Start()
{
rend = GetComponent<Renderer>();
}
void Update()
{
if (rend.IsVisibleFrom(Camera.main))
{
Debug.Log("#destroyer:isVisible: True");
}
else
{
object_roller.objSpawned = false;
Destroy(gameObject);
}
}
}
I would be SUPER thankful if somebody was able to help me I have no clue what does it and how to get rid of it.
Your answer
Follow this Question
Related Questions
Can I spawn with an array in order? 2 Answers
Random array with prefabs? 2 Answers
Multiplayer: 4 players, each player spawning less amount of other players. 0 Answers
Unity2D: How to destroy spawned object once it exit out of camera's view? 1 Answer
problem when i spawn to many Enemy the game be slower 1 Answer