Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity3d 达到某一点后不破坏的物体_Unity3d_Instantiation_Spawning - Fatal编程技术网

Unity3d 达到某一点后不破坏的物体

Unity3d 达到某一点后不破坏的物体,unity3d,instantiation,spawning,Unity3d,Instantiation,Spawning,好的,我正在制作一个无限跑者,我使用的技术是保持播放器静止,移动并实例化平台对象。 为此,我创建了一个作为平台的对象数组列表。 然后我将它们加入并繁殖它们。 我也在沿z轴平移它们,我想销毁z轴上低于0的对象,然后添加另一个替换对象。 问题是,除非我添加另一个脚本,否则它不会翻译对象,即使我添加另一个脚本进行翻译,它也不会破坏或添加对象 我的代码如下。如果你不能理解我的问题,请问我,我会再解释一遍 我有两个脚本。 1 PlatformManager:这个应用于空的游戏对象并包含ArrayList。

好的,我正在制作一个无限跑者,我使用的技术是保持播放器静止,移动并实例化平台对象。 为此,我创建了一个作为平台的对象数组列表。 然后我将它们加入并繁殖它们。 我也在沿z轴平移它们,我想销毁z轴上低于0的对象,然后添加另一个替换对象。 问题是,除非我添加另一个脚本,否则它不会翻译对象,即使我添加另一个脚本进行翻译,它也不会破坏或添加对象

我的代码如下。如果你不能理解我的问题,请问我,我会再解释一遍

我有两个脚本。 1 PlatformManager:这个应用于空的游戏对象并包含ArrayList。
public class PlatformManager : MonoBehaviour
{
    [HideInInspector]
    public List<GameObject> platforms = new List<GameObject>(); // List of Platfroms.
    public GameObject[] prefab; // Allow user to add as many prefabs through the inspactor.

    public static float noOfPlatforms;  // a variable to hold how many platoforms.
    [HideInInspector]
    public static float objectPosition; // Z position of the game object.
    [HideInInspector]
    public static float objectScale;

        // Use this for initialization
        void Start ()
        {
        noOfPlatforms = 6.0f;
        objectPosition = 0.0f;
        objectScale = 0.0f;


        platforms.Add((GameObject)Instantiate(prefab[Random.Range(0,prefab.Length)], new Vector3(0,0,0) ,Quaternion.identity));
        //platforms.Add((GameObject)Instantiate(prefab[Random.Range(0, prefab.Length)], new Vector3(0, 0, 40.40114f), Quaternion.identity));
        for(int i = 0; i < noOfPlatforms; i++){
            objectScale = platforms[platforms.Count-1].transform.localScale.z;
            objectPosition = platforms[platforms.Count-1].transform.localPosition.z;

            platforms.Add((GameObject)Instantiate(prefab[Random.Range(0,prefab.Length)], new Vector3(0,0,(objectPosition + objectScale)+277f) ,Quaternion.identity));
        }
        }

        // Update is called once per frame
        void Update ()
        {

        }
}
2平台控制器:这一个应该实例化、销毁和转换对象。
public class PlatformController : MonoBehaviour {
    //private bool canBeDestroy = true; //  Flag to check whether the gameObject shoud be destroyed or not.
    [HideInInspector]
    public PlatformManager managePlateform; //  Reference to plateformManager script.

    [HideInInspector]
    public float plateformSpeed = 10f;
    [HideInInspector]
    public GameObject allPlatforms;
    [HideInInspector]


    //  Awake is called when the script instance is being loaded.
    void Awake()
    {
        //  Accessing the plateformManager script for the Plateform.
        managePlateform = GameObject.FindGameObjectWithTag("Platform").GetComponent<PlatformManager>();
    }

    void Start()
    {

    }


    //  Update is called once per frame
    void Update()
    {

        //  Get the first platform object.
        GameObject firstPlatform = managePlateform.platforms[0];
        //  Get the last platform object.
        GameObject lastPlatform = managePlateform.platforms[managePlateform.platforms.Count - 1];

        if (firstPlatform.transform.localPosition.z < 0f)
        {
            Destroy(firstPlatform.gameObject); // destroy the first plateform gameObject.

            managePlateform.platforms.Remove(firstPlatform); // also remove the destroyed object from the list.

            // When the game object is destroyed then instantiate one gameobject into list and add at the last point.
            managePlateform.platforms.Add((GameObject)Instantiate(managePlateform.prefab[Random.Range(0, managePlateform.prefab.Length)], new Vector3(0, 0, (lastPlatform.transform.localPosition.z + lastPlatform.transform.localScale.z) + 277f), Quaternion.identity));
        }
        // Move the available platforms in the list along the z-axis
        foreach (GameObject platform in managePlateform.platforms)
        {
            platform.transform.Translate(0, 0, -plateformSpeed * Time.deltaTime);
        }
    }

}

您是否尝试过在预期冲突时调试代码?我不预期冲突,先生。我注意到您使用lastPlatform.transform.localPosition.z这是否意味着平台是另一个对象的子对象?在这种情况下,您需要通过transform.parent=…设置父级。您是否得到任何异常以及您的问题到底是什么?一切都正常吗?你只是想把脚本分开,然后按不同的顺序排列?你能更详细地解释一下你到底需要什么帮助吗?@T.Grumser我三年前问过这个问题。我不再创作游戏了,我甚至不明白我在这里问什么。所以我想,这个问题应该被标记为关闭,或者应该被删除,因为我没有得到答案。