生成几秒钟后如何破坏对象?

时间:2019-07-07 11:10:46

标签: c# unity3d

我无法销毁生成的克隆对象。它不会破坏它。

我正在使用预制件(健康,盔甲...)和空的游戏对象作为生成点。

它生成了它,一切都还好,但是我没有销毁它。所以我的问题是:如何消灭物体的孩子? (更新功能中的第二个“如果”!)。

import javax.swing.*;
import java.io.IOException;

public class App {

    public static void main(String[] args) {
        String segmentedImageDir="", segmentedImageSuffix="", originalImageDir="";
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setDialogTitle("Select Original Images Directory");
        System.out.println("Getting Original Images Directory");
        if(fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
            originalImageDir = fc.getSelectedFile().getAbsolutePath();
        }
        System.out.println("Original Images Directory: "+originalImageDir);
        fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setDialogTitle("Select Segmented Images Directory");
        System.out.println("Getting Segmented Images Directory");
        //Everything works fine till here
        if(fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
            segmentedImageDir = fc.getSelectedFile().getAbsolutePath();
        }
        System.out.println("Segmented Images Directory: "+segmentedImageDir);

        segmentedImageSuffix = MainFrame.getSegmentedImageSuffix();

        try{
            new MainFrame(originalImageDir, segmentedImageDir, segmentedImageSuffix);
        } catch (IOException ioe){
            ioe.printStackTrace();
            JOptionPane.showMessageDialog(null, "Could not load image", "IOException", JOptionPane.ERROR_MESSAGE);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

Destroy接受第二个参数来延迟有效销毁

public class SpawnObjects : MonoBehaviour
{

    public Transform pickUp;
    public Transform[] spawnPoints;
    public float Timer = 10;

    void Start()
    {
        if (spawnPoints.Length == 0)
        {
            Debug.LogError("No spawn points referenced.");
        }
    }

    void Update()
    {
        Timer -= Time.deltaTime;

        if (Timer <= 0)
        {
            Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
            GameObject instance = Instantiate(pickUp, _sp.position, _sp.rotation).gameObject;
            Destroy(instance, 15);
            Timer = 10;
        }
    }
}

答案 1 :(得分:0)

您可以在启动函数Destroy(gameObject, 15);中编写游戏对象,这是您要销毁的游戏对象,即15秒钟,您要使其保持活动状态。