Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k views
in Technique[技术] by (71.8m points)

c# - Respawn AI after destroy

I'm new, and I've got hard time to respawn my AI (right now he just a cube that follow my player) after he been destroy. I believe its because the script sits on the object that get destroyed. but what I need to do to respawn it?

(although I'm sure my respawn code is not good : (It's mobile-android project) )

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

public class EnemyTesting : MonoBehaviour
{

    [SerializeField]
    public GameObject player;
    public GameObject enemy;
    private Rigidbody body;

    Vector3 accelerationDir;

    // Use this for initialization
    void Start()
    {
        body = GetComponent<Rigidbody>();
    }
    private void Update()
    {
        accelerationDir = Input.acceleration;

        if (accelerationDir.sqrMagnitude>=5)
        {
            EnemyDead();
        }
    }

    void EnemyDead()
    {

        Destroy(enemy);
        Invoke("Respawn", 5);
    }
    void Respawn()
    {
        enemy = (GameObject)Instantiate(enemy);
        enemy.transform.position = transform.position;
    }
   

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 toTarget = player.transform.position - transform.position;
        float speed = 1.5f;

        transform.Translate(toTarget * speed * Time.deltaTime);
    }
}

Thanks very much!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I am supposing that your enemy GameObject is inside the scene holding your EnemyTesting MonoBehaviour instance (correct me if I'm wrong).

If this is the case, you cannot instantiate a gameObject that is destroyed.

As @derHugo pointed out, you should not use Destroy and Instantiate for your use case. It would be better to set inactive the enemy GameObject, move it to the position that you want, an (re)set it active. It will look like the enemy respawned.

If you want to dig the subject later, look at the object pooling game optimization pattern.


Otherwise, if you still want to use Instantiate for respawning, I would create a prefab of your enemy GameObject. The enemy GameObject referenced in the EnemyTesting field (in the Inspector view), would be your prefab from the project hierarchy instead of a GameObject inside the scene.

This way, you would be able to instantiate an enemy GameObject as many times as you want (and use it in other scenes!). Don't forget to hold a reference to the instantiated enemy GameObject so you can know which one you want to destroy. It would looke like this :

enemy = Instantiate(enemyPrefab, transform.position, transform.rotation);

You can replace transform with the transform of your choice, for example the transform of an empty enemyRespawnPoint GameObject in your Scene.

Do you see any error in the console ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...