How can i wait StartCoroutine to be finished in one script before starting a second script ?
The first script is using StartCoroutine i want to spawn the cubes one by one and not all the cubes at once so i'm using StartCoroutine:
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 should Find all the "Spawned Object" objects:
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");
But waypoints get only one object. The problem is since i'm using StartCoroutine in the first script it's doing the loop once then going to the second script and then back to the first script and finish the Coroutine.
Using Script Execution Order won't help here. The execution order is fine the problem is how to tell the second script to wait until the StartCoroutine on the first script is finished spawning all the cubes.
Answer by Cuttlas-U · Oct 26, 2017 at 06:11 AM
hi; u are doing it wrong; i can help u to fix it the right way ; but if u want to fix it fast for now u can change this lane and wait in Start Method;
IEnumerator Start()
{
yield return new WaitForSeconds(1); // wait as much as u like here then find the objects
waypoints = GameObject.FindGameObjectsWithTag("Spawned Object");
}
Hi,
This is kind of working but i'm still getting exceptions in the editor in the console until it find the objects. I'm waiting 10 seconds. Another problem with this solution is that you don't know how much to wait i did 10 seconds but it also depending on the first script on how much time i set there the StartCoroutine.
What other solution you might have ?
hi ; i said this is not a good fix;
better way is to make a function like this :
Public void FindAll()
{
waypoints = GameObject.FindGameObjectsWithTag("Spawned Object");
}
then when the corutine is over call this function ; so u are sure u are calling it just in time ;
IEnumerator Spawn()
{
for (int i = 0; i < numberOfObjects; i++)
{
// your spawn code here
}
GameObject.FindObjectOfType(WayPoints).FindAll(); // we call the function when spawning is over
}
Your answer
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
Creating Splines from empties in script 0 Answers
How can i create List of maps from each Map class ? 0 Answers
How can i rotate all the child objects together at the same time ? 1 Answer
How can i give another name/number to the created Plane object name ? 0 Answers