【问题标题】:Unity script not doing anything, but dont get any errors eitherUnity脚本没有做任何事情,但也没有得到任何错误
【发布时间】:2015-01-29 23:58:17
【问题描述】:

大家好,总的来说,我对统一和编码还很陌生,但我真的试图弄清楚为什么这个脚本不适用。该脚本的想法是应该在 X 轴上的随机位置上重复生成一个对象,范围为 +9 到 -9,但它只会在对象的起始位置生成一次。希望有人能指出我正确的方向:)

using UnityEngine;
using System.Collections;

public class SpawnOnXaxis : MonoBehaviour {

public GameObject Goodfood;
public int numToSpawn;
public Vector3 position;

void Awake()        
{
    Vector3 position = new Vector3(Random.Range(-9.0F, 9.0F), 10.5f, -1); // -9 på Xaxis - +9 ----- Y = 10.5 z = -1
}

void Start() 
{
    int spawned = 0;

    while (spawned < numToSpawn)
    {

        position = new Vector3(Random.Range(-9.0F, 9.0F), 10.5f, Random.Range(-9.0F, 9.0F));

        GameObject tmp = Instantiate(Goodfood, position, Quaternion.identity) as GameObject; // Quaternion.identity betyder "ingen rotation"

        spawned++;
        System.Threading.Thread.Sleep(500);
    }
}

}

【问题讨论】:

    标签: c# random unity3d 2d


    【解决方案1】:

    您没有将 numToSpawn 设置为任何值。试试这个:

    public class SpawnOnXaxis : MonoBehaviour {
    
    public GameObject Goodfood;
    public int numToSpawn;
    public Vector3 position;
    
    void Awake()        
    {
        position = new Vector3(Random.Range(-9.0F, 9.0F), 10.5f, -1); // -9 på Xaxis - +9 ----- Y = 10.5 z = -1
        numToSpawn = 10;
    }
    
    void Start() 
    {
        int spawned = 0;
    
        while (spawned < numToSpawn)
        {
    
            position = new Vector3(Random.Range(-9.0F, 9.0F), 10.5f, Random.Range(-9.0F, 9.0F));
    
            GameObject tmp = Instantiate(Goodfood, position, Quaternion.identity) as GameObject; // Quaternion.identity betyder "ingen rotation"
    
            spawned++;
            System.Threading.Thread.Sleep(500);
        }
    }
    }
    

    【讨论】:

    • 没什么区别 :/ 除了在游戏开始时生成的那个之外什么都没有发生
    • 是否调用了 Awake() 函数?在声明时尝试将 numToSpawn 设置为例如 10:public int numToSpawn = 10;
    【解决方案2】:

    试试这个。 Invoke Reapeating 可以解决您的问题。 下面的代码每 0.3 秒调用一次 CreateObjects。首先 1 秒后它开始。

    public GameObject Goodfood;
    public int numToSpawn;  //I assume you edit this value in editor
    public Vector3 position;
    int spawned = 0;
    void Start() // or you can use Awake too.
    {
        InvokeRepeating("CreateObjects", 1, 0.3);
    }
    
    void CreateObjects(){
         position = new Vector3(Random.Range(-9.0F, 9.0F), 10.5f, Random.Range(-9.0F, 9.0F));
         GameObject tmp = Instantiate(Goodfood, position, Quaternion.identity) as GameObject;
         spawned++;
         if(spawned>9){
            CancelInvoke("CreateObjects");
         }
    }
    

    如果你不想使用这个,也许你可以使用Update 方法。喜欢

     void Update() {
         if(spawned<10){
             position = new Vector3(Random.Range(-9.0F, 9.0F), 10.5f, Random.Range(-9.0F, 9.0F));
             GameObject tmp = Instantiate(Goodfood, position, Quaternion.identity) as GameObject;
             spawned++;
         }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2012-09-07
      • 2016-10-02
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多