Making clones of a GameObject but then the original gets destroyed
So, I'm trying to make a SpaceInvaders-type game, and I made a gameObject, called "Enemy1", which has a script that tells him to follow a specific path;
I then made a spawner, that makes (trough Instantiate command) some "Enemy1" clones. This spawner, makes a clone every 1 second, for 5 seconds, so it should spawn 4-5 clones, with the original "Enemy1" gameObject, all of them creating a queue in this path.
Everything works just fine, the only problem is that, if while the clones are spawning, I try to "kill" the original gameObject, the other clones stop spawning, because they have no more reference to the original "Enemy1".
The problem is that these enemies move because the path script is inside them so, if I try to make just a reference for the clones that is stationary and not in the scene so it can't being killed, all the clones will spawn like him, and will not move.
How can I make a reference for the clones that cannot being killed, but still tell them to move following the path?
Thanks, I'm really struggling with this one.
Here is the spawner script:
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
private float currentTimeToSpawn;
public float timeToSpawn;
public float maxTimeToSpawn = 5;
public Transform pointA;
public GameObject enemy;
// Update is called once per frame
void Update()
{
if(maxTimeToSpawn > 0)
{
if(currentTimeToSpawn > 0)
{
currentTimeToSpawn -= Time.deltaTime;
}
else
{
SpawnObject();
currentTimeToSpawn = timeToSpawn;
maxTimeToSpawn--;
}
}
}
void SpawnObject()
{
Instantiate(enemy, pointA.position, Quaternion.identity);
}
}
aaand I'm not sure it's useful, but here's the path script that should be in every enemy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interpolation : MonoBehaviour
{
[SerializeField] private Transform pointA;
[SerializeField] private Transform pointB;
[SerializeField] private Transform pointC;
[SerializeField] private Transform pointD;
[SerializeField] private Transform pointAB;
[SerializeField] private Transform pointBC;
[SerializeField] private Transform pointCD;
[SerializeField] private Transform pointAB_BC;
[SerializeField] private Transform pointBC_CD;
[SerializeField] private Transform pointABCD;
private float interpolateAmount;
// Update is called once per frame
private void Update()
{
interpolateAmount = (interpolateAmount + Time.deltaTime/6);
/*pointAB.position = Vector3.Lerp(pointA.position, pointB.position, interpolateAmount);
pointBC.position = Vector3.Lerp(pointB.position, pointC.position, interpolateAmount);
pointCD.position = Vector3.Lerp(pointC.position, pointD.position, interpolateAmount);
pointAB_BC.position = Vector3.Lerp(pointAB.position, pointBC.position, interpolateAmount);
pointBC_CD.position = Vector3.Lerp(pointBC.position, pointCD.position, interpolateAmount);
pointABCD.position = Vector3.Lerp(pointAB_BC.position, pointBC_CD.position, interpolateAmount); */
pointABCD.position = CubicLerp(pointA.position, pointB.position, pointC.position, pointD.position, interpolateAmount);
}
private Vector3 QuadraticLerp(Vector3 a, Vector3 b, Vector3 c, float t)
{
Vector3 ab = Vector3.Lerp(a, b, t);
Vector3 bc = Vector3.Lerp(b, c, t);
return Vector3.Lerp(ab, bc, interpolateAmount);
}
private Vector3 CubicLerp(Vector3 a, Vector3 b, Vector3 c, Vector3 d, float t)
{
Vector3 ab_bc = QuadraticLerp(a, b, c, t);
Vector3 bc_cd = QuadraticLerp(b, c, d, t);
return Vector3.Lerp(ab_bc, bc_cd, interpolateAmount);
}
}
Your answer
Follow this Question
Related Questions
Instantiate problem, spawning too many clones, then cloning the clones! help! 0 Answers
Issue with instantiated text clone 0 Answers
Destroy the first instantiated clone after 4 have been instantiated 1 Answer
Saving Instantiated objects,Saving Instantiated objects 0 Answers
Exponential Instantiation 0 Answers