Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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 ?

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda