同じ空のオブジェクトに2つのスクリプトが添付されています。 最初のスクリプトは次のとおりです。まだ1つのスクリプトを実行しているスクリプトの順序の設定が最初に実行されているのはなぜですか?
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);
}
}
2番目のスクリプト:
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");
最初のスクリプトは、新しい10個のキューブを生みます。
for (int i = 0; i < numberOfObjects; i++)
: しかし、2番目のスクリプトは、私はそれが一度ループでやって最初のスクリプトを実行していていることがわかりブレークポイントを使用する場合、変数は1つだけ「スポーンオブジェクト」といない10
を得ているウェイポイントしかし、それは2番目のスクリプトにジャンプして1つの "Spawned Object"を取得し、最初のスクリプトに戻り、残りのループを作ります。
そして、私が望むのは、最初にループ全体を2つ目のスクリプトで10個のキューブにすることです。
私はエディタでも編集:プロジェクト設定スクリプト実行順序 それでも、ループ全体を処理していません。
yield return new WaitForSeconds(spawnSpeed);
Start()
関数が正しい順序で呼び出されているが、最初のスクリプトは、ちょうど各反復の後に得コルーチンを開始します