【问题标题】:spawn gameobject from array从数组中生成游戏对象
【发布时间】:2016-02-27 11:43:20
【问题描述】:

我在从 for 循环中的数组生成随机小行星时遇到了一些麻烦。

该数组现在正在工作,但是(我假设这是在某处的 for 循环中)它不会在不同的游戏对象之间交替。无论哪个对象先生成,每次都会生成,但每次加载游戏时都会生成不同的对象。

如何让它在每次生成实例后从数组中选择一个新的随机对象?

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {

    public GameObject[] asteroids;
    public Vector3 spawnValues;
    public int asteroidCount;
    public float spawnWait;
    public float startWait;
    public float waveWait;

    void Start () {
        StartCoroutine (spawnWaves ());
    }

    IEnumerator spawnWaves () {

        GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];

        while (asteroidCount > 0) {
            for (int i = 0; i < asteroidCount; i++) {
                Vector3 spawnPosition = new Vector3 (spawnValues.x, Random.Range (-spawnValues.y, spawnValues.y), spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate (asteroid, spawnPosition, spawnRotation);
                yield return new WaitForSeconds (spawnWait);
            }
        }
    }
}

【问题讨论】:

  • 离题了,但是你是说用 C# 标记而不是 C?我不知道你是否能够将 C 与 unity 结合使用
  • 我的错,不应该是c#!

标签: c# arrays random unity3d unity5


【解决方案1】:

您选择要在循环外生成的对象

GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];

需要在这里

   while (asteroidCount > 0) {
        for (int i = 0; i < asteroidCount; i++) {
          GameObject asteroid = asteroids[Random.Range(0, asteroids.Length)];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多