Reputation: 11335
I have two scripts attached to the same emptygameobject. The first script is:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SpawnObjects : MonoBehaviour
{
public int numberOfObjects;
public GameObject objectToPlace;
public Vector3 newObjectsSize = new Vector3(5, 5, 5);
public float spawnSpeed = 0.1f;
private int wallsLengthX;
private int wallsLengthZ;
private int wallsPosX;
private int wallsPosZ;
private int currentObjects;
private List<GameObject> objects = new List<GameObject>();
void Start()
{
var wi = GetComponent<Walls>();
wallsLengthX = (int)wi.lengthX;
wallsLengthZ = (int)wi.lengthZ;
wallsPosX = (int)wi.wallsStartPosition.x;
wallsPosZ = (int)wi.wallsStartPosition.z;
StartCoroutine(Spawn());
}
IEnumerator Spawn()
{
for (int i = 0; i < numberOfObjects; i++)
{
GameObject newObject = (GameObject)Instantiate(objectToPlace);
newObject.transform.localScale = new Vector3(newObjectsSize.x, newObjectsSize.y, newObjectsSize.z);
newObject.transform.localPosition = GenerateRandomPositions(newObject);
newObject.name = "Spawned Object";
newObject.tag = "Spawned Object";
objects.Add(newObject);
yield return new WaitForSeconds(spawnSpeed);
currentObjects += 1;
}
}
private Vector3 GenerateRandomPositions(GameObject newObject)
{
float paddingX = Mathf.Clamp(newObject.transform.localScale.x, 0, wallsLengthX) / 2f;
float paddingZ = Mathf.Clamp(newObject.transform.localScale.z, 0, wallsLengthZ) / 2f;
float originX = wallsPosX + paddingX - wallsLengthX / 2f;
float originZ = wallsPosZ + paddingZ - wallsLengthZ / 2f;
float posx = UnityEngine.Random.Range(originX, originX + wallsLengthX - paddingX);
float posz = UnityEngine.Random.Range(originZ, originZ + wallsLengthZ - paddingZ);
float posy = Terrain.activeTerrain.SampleHeight(new Vector3(posx, 0, posz));
return new Vector3(posx, posy, posz);
}
}
The second script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WayPoints : MonoBehaviour
{
public GameObject[] waypoints;
public Transform target;
public float moveSpeed = 10f;
public float moveSpeed1 = 10f;
public float slowDownSpeed = 3f;
public float reverseSlowDownSpeed = 3f;
public float rotationSpeed = 1f;
private Transform myTransform;
private int targetsIndex = 0;
private Vector3 originalPosition;
private GameObject[] robots;
public Transform reverseTarget;
private int reverseTargetsIndex = 0;
private Vector3 reverseOriginalPosition;
public bool random = false;
void Awake()
{
myTransform = transform;
}
// Use this for initialization
void Start()
{
waypoints = GameObject.FindGameObjectsWithTag("Spawned Object");
The first script spawn new 10 cubes. But the second script waypoints variable is getting only 1 "Spawned Object" and not 10.
When i used a break point i see that it's executing the first script doing in the loop once:
for (int i = 0; i < numberOfObjects; i++)
But then it's jumping to the second script getting one "Spawned Object" and then back to the first script and make the rest of the loop.
And what i want is that it will make first the whole loop spawn 10 cubes thne to get them all with the second script.
I even did in the editor: Edit > Project Settings > Script Execution Order But still it's not doing first the whole loop only one iterate.
Upvotes: 1
Views: 45
Reputation: 9821
You have a yield in Spawn()
coroutine:
yield return new WaitForSeconds(spawnSpeed);
The Start()
functions are being called in the correct order but the first script is just starting a coroutine that yields after each iteration. After the first yield, it's going to wait to continue until that WaitForSeconds
ends; in the mean-time, the second script will run its Start()
.
As a solution, you aren't going to be able to fix this with script execution order. Your second script will have to wait until the first script is done running its Spawn()
coroutine. There are plenty of ways to do this but the easiest would be to either expose the Coroutine
object returned by StartCoroutine()
or make something like a bool DoneSpawning
flag in your SpawnObjects
class.
Upvotes: 5