简体   繁体   中英

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. 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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