- Home /
instantiate random prefabs based on player camera distance
I made a few prefabs I'd like to use in my project, but I don't really understand where this takes place, before starting the game itself (pressing Play on the menu) to trigger the instantiate, or during the game attach it to your player and check how much the player sees and if needed to generate more prefabs.
Answer by Digital-Phantom · Mar 20, 2015 at 04:52 PM
simply make an empty game object (placed in your scene) have it spawn/instantiate your random prefabs at the start of the scene. (do this in the Awake function of the empty object)
Alternatively (and a not really the best solution) have a large sphere collider attached to your player. Have this set to the same size/distance as your player can see. Place empty game objects at strategic points around your scene and when the player collider collides with these have them spawn more prefabs.
is there maybe a way to check the amount of prefabs created and left, like when the player is on the 3rd prefab, make 3 more prefabs ahead and destroy the ones the player has passed already
in short... yes.
make an int variable(variable with whole number, not decimal). Something like
public int prefabNum;
then later in your script, each time you add/instantiate a prefab into your scene add 1 to prefabNum. (prefabNum += 1)
You can use a simple IF statement to check how many prefabs you have in the scene and change things from there.
If(prefabNum
That's a rough explaination of how to do it by prefab numbers. If you use my second suggestion of a large collider you can simply use OnTriggerExit to destroy any prefabs that were in your proximity but have moved outside of your collider.
Your answer
Follow this Question
Related Questions
instantiate problem 1 Answer
Can't set the random position for an Instance. 1 Answer
How To NOT Instantiate the same Prefab Twice (C#) 1 Answer
instantiates in the same place 2 Answers
instantiating vertically 2 Answers