Now the game I’m working on is supposed to spawn in enemies, which is done by instantiating a prefab. Now this all works fine, the issue is that when you try to kill one of the copies, only the original enemy is killed and deleted off the game, which means the copies doesn’t function. How do I fix it so you can only kill the copies and not the off-screen original that only serves as a prefab? (Script below)
//script for spawning in more enemies:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BotSpawner : MonoBehaviour
{
public Rigidbody bot;
public Transform spawnLocation;
public EnemyController ec;
public WeaponController wc;
public AudioClip SwordHitSound;
private void OnTriggerEnter(Collider other)
{
if(wc.IsAttacking == true && other.tag == "Weapon")
{
Debug.Log("Bot spawned!");
Instantiate(bot, spawnLocation.position, spawnLocation.rotation);
ec.IsClone = true;
AudioSource ac = GetComponent<AudioSource>();
ac.PlayOneShot(SwordHitSound);
}
}
//script for killing off enemies:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionDetection : MonoBehaviour
{
public WeaponController wc;
public EnemyController ec;
public AudioClip SwordHitSound;
private void OnTriggerEnter(Collider other)
{
if(wc.IsAttacking == true && ec.IsClone == true && other.tag == "Enemy")
{
ec.GetComponent<EnemyController>().die();
AudioSource ac = GetComponent<AudioSource>();
ac.PlayOneShot(SwordHitSound);
}
}
}
//script for turning enemies into ragdolls and then deleting them off the game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour
{
public bool IsClone;
// Start is called before the first frame update
void Start()
{
setRigidbodyState(true);
setColliderState(false);
IsClone = false;
}
// Update is called once per frame
void Update()
{
}
public void die()
{
GetComponent<Animator>().enabled = false;
setRigidbodyState(false);
setColliderState(true);
Destroy(gameObject, 5f);
}
void setRigidbodyState(bool state)
{
Rigidbody[] rigidbodies = GetComponentsInChildren<Rigidbody>();
foreach(Rigidbody rigidbody in rigidbodies)
{
rigidbody.isKinematic = state;
}
GetComponent<Rigidbody>().isKinematic = !state;
}
void setColliderState(bool state)
{
Collider[] colliders = GetComponentsInChildren<Collider>();
foreach (Collider collider in colliders)
{
collider.enabled = state;
}
GetComponent<Collider>().enabled = !state;
}
}
[CODE]