gpt4 book ai didi

c# - 为什么在设置脚本执行顺序时仍然先执行一个脚本?

转载 作者:太空宇宙 更新时间:2023-11-03 19:44:59 27 4
gpt4 key购买 nike

我有两个脚本附加到同一个空游戏对象。第一个脚本是:

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);
}
}

第二个脚本:

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 个新的立方体。但是第二个脚本航路点变量只得到 1 个“Spawned Object”而不是 10 个。

当我使用断点时,我看到它正在执行循环中的第一个脚本:

for (int i = 0; i < numberOfObjects; i++)

但随后它跳转到第二个脚本获取一个“生成的对象”,然后返回到第一个脚本并进行剩余的循环。

我想要的是,它首先让整个循环生成 10 个立方体,然后用第二个脚本将它们全部获取。

我什至在编辑器中做了:Edit > Project Settings > Script Execution Order但它仍然没有首先执行整个循环,只进行一次迭代。

最佳答案

您的 yield 为 Spawn()协程:

yield return new WaitForSeconds(spawnSpeed);

Start()以正确的顺序调用函数,但第一个脚本只是启动一个协程,该协程在每次迭代后产生。在第一次产量之后,它将等待继续直到 WaitForSeconds结束;同时,第二个脚本将运行其 Start() .

作为解决方案,您无法通过脚本执行顺序来解决此问题。您的第二个脚本必须等到第一个脚本运行完其 Spawn()协程。有很多方法可以做到这一点,但最简单的方法是公开 Coroutine StartCoroutine() 返回的对象或制作类似 bool DoneSpawning 的东西在你的 SpawnObjects 中标记类。

关于c# - 为什么在设置脚本执行顺序时仍然先执行一个脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46942692/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com