My platform spawner script isn’t working as planed,
I have 2 times 3 diferent platformes i’d like to spawn, it should look like this for example :
({}{< “spaces” >}{}ect…)
But for now it only spawns the and not the whole block,
So could you peek into my code please and tell me what’s going wrong ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class platformesSpawner : MonoBehaviour
{
public GameObject smallPlatformStart;
public GameObject smallPlatformMiddle;
public GameObject smallPlatformEnd;
public GameObject tallPlatformStart;
public GameObject tallPlatformMidle;
public GameObject tallPlatformEnd;
float timer;
void Start()
{
}
bool RandomNbr()
{
Debug.Log(new System.Random().Next(100) % 2 == 0);
return new System.Random().Next(100) % 2 == 0;
}
void SmallPlatformSpawner()
{
Instantiate(smallPlatformStart, transform.position, transform.rotation);
while (RandomNbr())
Instantiate(smallPlatformMiddle, transform.position, transform.rotation);
Instantiate(smallPlatformEnd, transform.position, transform.rotation);
}
void TallPlatformSpawner()
{
Instantiate(tallPlatformStart, transform.position, transform.rotation);
while (RandomNbr())
Instantiate(tallPlatformMidle, transform.position, transform.rotation);
Instantiate(tallPlatformEnd, transform.position, transform.rotation);
}
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
if (!RandomNbr())
SmallPlatformSpawner();
else
TallPlatformSpawner();
timer = 2;
}
}
}
The random number function confuses me. So i might not be much help.
i think the while loop is an infinite loop. but you’re not complaining about that. i don’t even understand why you have that while loop. I would remove it.
the Update() here is thought on the flow
timer -= Time.deltaTime; // subtract time
if (timer <= 0) // if time is less or equal to 0
{
if (!RandomNbr()) //if random is not = 0
SmallPlatformSpawner(); // spawn this
else
TallPlatformSpawner(); // if random is not = 0 then do this
timer = 2; // set the time back to 2, start count down again
}
Thanks for you answers,
So i took the loops out for now, and the problem seem’s to be diferent now haha
Some of my platforms are acually spawning on top of each others, is there a way to make sure that they spawn on after the other ?
Like wait for the end of an “Instantiate” to lanch the second “Instantiate”?
so the reason why they are spawning on top of one another is because the prefab has the same transform info.
there are two ways to fit it.
1: set the transform info in each prefab to the position you want it in the game.
2: when you spawn the prefab you also set it position.
I always use option 2: so have more control over the object.
void SmallPlatformSpawner()
{
GameObject PlatformRoot;
if (GameObject.Find("PlatformRoot")) //check if root object has been created
{
PlatformRoot = GameObject.Find("PlatformRoot"); // root was already created to set it
}
else
{
//root was not created, so lets create it
PlatformRoot = new GameObject();
PlatformRoot.name = "PlatformRoot";
PlatformRoot.transform.position = new Vector3(0,0,0);
}
GameObject PlatformStart = Instantiate(smallPlatformStart, transform.position, transform.rotation); //spawn the object and set a referance to it
PlatformStart.transform.SetParent(PlatformRoot.transform); //set it to be a child of the root object
//PlatformStart.transform.localPosition = new Vector3(1, 0, 0); // set its local position
PlatformStart.transform.position = new Vector3(1, 0, 0); // or world position
GameObject PlatformMid = Instantiate(smallPlatformMiddle, transform.position, transform.rotation); //spawn the object and set a referance to it
PlatformMid.transform.SetParent(PlatformStart.transform); //set it to be a child of the start object
//PlatformMid.transform.localPosition = new Vector3(1, 0, 0); // set its local position
PlatformMid.transform.position = new Vector3(0, 0, 3); // or world position
GameObject PlatformEnd = Instantiate(smallPlatformEnd, transform.position, transform.rotation); //spawn the object and set a referance to it
PlatformEnd.transform.SetParent(PlatformMid.transform); //set it to be a child of the mid object
//PlatformEnd.transform.localPosition = new Vector3(1, 0, 0); // set its local position
PlatformEnd.transform.position = new Vector3(5, 0, 0); // or world position
}
I just tried it ( moved somethings here and there ) and that’s exactly what i was wanting to do, thx !
But i have a small question about the code now, where do the platforms spawn ? Do they spawn from the platformRoot GameObject or from the GameObject i created in unity ? Because right now there kind of spawning out of nowhere when i play the scene haha.