简体   繁体   English

为什么 unity3d 克隆没有被销毁

[英]why are unity3d clones not destroyed

In this code, I clone objects as they approach the player's origin, and destroy them when they get past it.在这段代码中,我在对象接近玩家原点时克隆它们,并在它们经过它时销毁它们。

This script is attached to two game objects.此脚本附加到两个游戏对象。

When the game is played, the two original object disappear from the hierarchy when they are destroyed.游戏进行时,原来的两个object被破坏时从层次结构中消失。 When the clones are destroyed, they disappear from the game screen, but remain in the hierarchy.当克隆体被摧毁时,它们会从游戏画面中消失,但仍保留在层级中。

I assume this is a problem.我认为这是一个问题。 How can I fix it?我该如何解决?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveTraffic : MonoBehaviour
{
    
    private float vehicleSpeed = 8.0f;
    private Vector3 startPosition;
    private bool needsDuplicate = true;
    // Start is called before the first frame update
    void Start()
    {
        startPosition = transform.position;
        // force startPosition Z to be at the edge of the road;
        startPosition.z = 178.0f;
    }

    // Update is called once per frame
    void Update()
    {
        // Move the vehicle forward or backward
        transform.Translate(Vector3.forward * Time.deltaTime * vehicleSpeed);

        //create duplicates at certain points along the road, starting them back at startPosition.
        var pz = transform.position.z;
        if (needsDuplicate)
        {
            //if ((pz < 178f * .75 && pz > 178f * .7) || (pz < 178 * .5 && pz > 178f* .4))
            if (pz < 178 * .5 && pz > 178f * .4)
            {
                Instantiate(this, startPosition, transform.rotation);
                needsDuplicate = false;
            }
        }
        else
        {
            //if ((pz < 178f * .7 && pz > 178f * .6) || (pz < 178 * .5 && pz > 178f * .6))
            if (pz < 178 * .5 && pz > 178f * .6)
            {
                needsDuplicate = true;
            }
        }
            
        //Respawn and destroy when it gets to the end of the road.
        if (transform.position.z < -2)
        {
            //transform.position = new Vector3(transform.position.x, transform.position.y, restartZ);
            Instantiate(this, startPosition, transform.rotation);
            Destroy(this.gameObject);
        }
    }
}

The issue is not that the clone is not getting destroyed.问题不在于克隆没有被摧毁。 Each clone you instantiate is creating a duplicate of itself(probably overlapping), giving the illusion that its disappearing from the scene, but not from the hierarchy.您实例化的每个克隆都在创建自身的副本(可能重叠),给人一种它从场景中消失而不是从层次结构中消失的错觉。 If you double click any of the clone that are supposedly not getting destroyed, you will see the exact location of that clone in the scene mode.如果你双击任何一个应该没有被破坏的克隆体,你将在场景模式中看到该克隆体的确切位置。

In my opinion, if you want only two cars in the scene, you don't have to clone the transform every time you Destroy it.在我看来,如果场景中只需要两辆车,则不必在每次销毁它时都克隆变换。

//Respawn and destroy when it gets to the end of the road.
if (transform.position.z < -2)
{
  Destroy(this.gameObject);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM