Hi guys i am stuck on a c sharp problem … can any one tell me how to spawn a prefab multiple time after some interval … i created a script in which i create a method that returns a vector3 (Random position for the prefab) and also made a coroutine and add instantiate in it with a random position … but when i play the game it just spawn the prefab once …plzz help me…
2 Answers
2You are only calling Delay() in Start(), which only runs once, try something like this:
private CharacterController player;
public float MinX , MinY , MaxX , MaxY , MinZ , MaxZ;
public float moveForce = 3f;
public GameObject prefab;
private FixedJoystick joystick;
private Vector3 randompos;
public int numberToSpawn;
void Start () {
player = GetComponent<CharacterController> ();
joystick = GameObject.FindWithTag("Joystick").GetComponent<FixedJoystick>();
randompos = random ();
StartCoroutine (Delay());
}
Vector3 random() {
float Ranx = Random.Range(MinX,MaxX);
float Rany = Random.Range (MinY,MaxY);
float Ranz = Random.Range (MinZ, MaxZ);
return new Vector3 (Ranx , Rany , Ranz);
}
IEnumerator Delay() {
if(numberToSpawn < 1)
{
yield break;
}
yield return new WaitForSeconds (1f);
Instantiate (prefab, randompos, Quaternion.identity);
}
void Update () {
Vector3 move = new Vector3(joystick.Horizontal ,0, joystick.Vertical );
player.Move(move * Time.deltaTime * moveForce);
if (move != Vector3.zero)
transform.forward = move;
if(joystick.Horizontal != 0f || joystick.Vertical != 0f) {
transform.rotation = Quaternion.LookRotation(move) ;
}
}
I added a public int numberToSpawn that you will need to setup in inspector, and I should let you know I don’t have a compiler or Unity so it is not tested, but that should work.
EDIT:
Alright, back home from work with Unity, my last answer was incomplete and didn’t exactly work either… but I got one that works!
@sacredgeometry , I’m sure your method also works, but wow that is a very high level solution for someone new to coding, and I have something a lot closer to what OP was originally using.
All the variables in your script that aren’t in the Update() function are confusing and there is a cleaner/smoother way to handle all the floats for your Vector3, but this code I wrote has those values in mind and I would try to find a better way to set the min and max for x,y,z than publicly in the inspector:
IEnumerator Delay()
{
while (numberToSpawn > 0)
{
Vector3 randompos = new Vector3(Random.Range(MinX, MaxX), Random.Range(MinY, MaxY), Random.Range(MinZ, MaxZ));
yield return new WaitForSeconds(1f);
Instantiate(prefab, randompos, Quaternion.identity);
numberToSpawn--;
}
}
Tested with Unity and that will work, just make sure you publicly declare the numberToSpawn; with this script you don’t need to call Vector3 random() anymore.
If you are using >=c#7 something like this perhaps?
void Start()
{
// Method Syntax
DelayedRepeater(4, 10, Create);
// Lambda Syntax
DelayedRepeater(4, 10, (index) => {
Debug.Log($"Run create the code for item: {index} here");
});
}
void Create(int index)
{
Debug.Log($"...or run the create code for item: {index} here");
}
void DelayedRepeater(float time, int repeat, Action<int> func)
{
IEnumerator coroutine (float t, int r, Action<int> f) {
yield return new WaitForSeconds(t);
for (int i = 0; i < r; i++)
{
f.Invoke(i);
}
};
StartCoroutine(coroutine(time, repeat, func));
}
how are we going to help without seeing the code
– xxmarioferHow does the script know how many to spawn? What method tells this coroutine run? ---------- There could be a few reasons why you are having trouble, so can you provide the script that you have all this inside of?
– I_Am_Err00r