【问题标题】:After destroying a prefab and then recreating the prefab, the new clone objects are not moving销毁预制件然后重新创建预制件后,新的克隆对象不会移动
【发布时间】:2015-10-16 14:09:39
【问题描述】:

我有代码可以生成一个向左、向右移动并落下的旋转球。生成点(空物体)设置在离地面约 3 个单位的位置,因此它会落到地面并左右移动。 我面临的问题是,在我销毁预制件的克隆并将其重置为在原始位置生成并期望它掉落到地面并移动后,它只会在原始位置生成并且不会掉落到地面。任何帮助将不胜感激。谢谢你。 `

public class spawnLemmings : MonoBehaviour {

 //this is the number of lemmings that will spawn
 private int numLemming = 6;

 //Creating a prefab gameObject to attach the prefab to
 public GameObject prefab;

 // Use this for initialization
 void Start () {
    Spawn();
 }
 void FixedUpdate()
 {
    Reset();
 }

 void Spawn()
 {
    //A for loop to create desired number of Lemmings
    for (int i = 0; i < numLemming; i++)
    {
        //This is where the game object is being created
        prefab = (GameObject)Instantiate(prefab, transform.position, Quaternion.identity);


        //This sets the speed of lemming.
        //prefab.GetComponent<lemmingMovement>().speed = Random.Range(3.0f, 4.0f); ///speed = 2.0f;

        //This resets onGround to false
        //prefab.GetComponent<lemmingMovement>().OnGround();
    }
 }
 void Reset()
 {
    if (Input.GetKeyDown("r"))
    {
        GameObject[] lems = GameObject.FindGameObjectsWithTag("Lemmings");
        foreach (GameObject lem in lems)
        {
            Destroy(lem);
        }
        Spawn();
    }
 }
}

`

【问题讨论】:

  • 对不起,我忘记删除 FixedUpdate(),我只是在测试是否是导致问题的原因。我的代码使用 Update()。

标签: c# unity3d


【解决方案1】:
prefab = (GameObject)Instantiate(prefab, transform.position, Quaternion.identity);

您将创建的新对象分配给用于克隆对象的预制件

试试这个

//This is where the game object is being created
GameObject o = (GameObject)Instantiate(prefab, transform.position, Quaternion.identity);

【讨论】:

  • 欢迎。您可以通过选择此作为答案将此问题标记为已解决
【解决方案2】:

据我所知,我在下面说明-

  1. 我不知道你的整个代码是如何工作的,但是从 cmets 我认为你应该通过取消注释代码来设置速度 -

    //This sets the speed of lemming.
    //prefab.GetComponent<lemmingMovement>().speed = Random.Range(3.0f, 4.0f); ///speed = 2.0f;
    
  2. 如果对象是刚体,则检查新创建的预制件/克隆是否也具有刚体。有时我忘记在最新更改后更新我的预制件。

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多