- Home /
Question by
Keiji1 · Oct 22, 2021 at 04:57 PM ·
script.scripting beginner
Static platform spawn at a fixed position if it spawn first
I make this script to spawn 2 prefabs in my game using a list of arrays. So to sum up the problem that I have right now is that I wanted my first prefabs (1st array object) on a static platform if it is the first object getting spawned when the game started it should spawn at a fixed x-axis coordinate if not then it should spawn randomly afterward.
float dirY;
public Platform[] prefabs;
public GameObject Player;
bool spawnEdit;
int random;
bool isFirstObject;
// Start is called before the first frame update
void Start()
{
dirY = -1.1f;
}
// Update is called once per frame
void Update()
{
if (Player.transform.position.y < dirY && !spawnEdit) {
for (int i = 0; i < 5; i++)
{
random = Random.Range(0, prefabs.Length);
GameObject Spawn = GameObject.Instantiate(prefabs[random].gameObject);
//if (random == 0)
//{
// Spawn.transform.position = new Vector2(1.5f, dirY);
//}
if(random == 0)
{
Spawn.transform.position = new Vector2(Random.Range(-1.5f, 1.5f), dirY);
}
else
{
Spawn.transform.position = new Vector2(0, dirY);
}
dirY += 1f;
Debug.Log(Player.transform.position.y+ " " + dirY);
}
spawnEdit = true;
}
else if (Player.transform.position.y > dirY-2) {
spawnEdit = false;
}
Comment
Your answer
Follow this Question
Related Questions
How do I make a photography function? 0 Answers
Show speed in UI 1 Answer
How to keep the old line text? 1 Answer
how can you make a 3D model slowly become transparent? 2 Answers
loop vs if spell casting script 1 Answer