- Home /
cant kill more than one enemy C#
simpy put using the script below i cant shoot after i kill the first enemy because of an issue at lines 42 and 75, both lines say "EnemyAi enemyAi = enemy.GetComponent();" and the exact error is "The object of type 'EnemyAi' has been destroyed but you are still trying to access it." can anybody help with this?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PistolRay : MonoBehaviour {
bool canFire = true;
public GameObject decal;
public float clipAmmo = 8;
public float storedAmmo = 80;
float missingShots = 0;
GameObject player;
GameObject enemy;
GameObject characterControler;
bool reloading = false;
private List<EnemyAi> enemies = new List<EnemyAi>();
// Use this for initialization
void Start () {
player = GameObject.Find("Player");
enemy = GameObject.Find("EnemyAI");
}
// Update is called once per frame
void Update () {
CharacterControler characterControler = player.GetComponent<CharacterControler>();
characterControler.ammoCount = clipAmmo;
characterControler.totalAmmo = storedAmmo;
var enemyObjects = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemyObj in enemyObjects){
EnemyAi enemyAi = enemyObj.GetComponent<EnemyAi>();
if(enemyAi != null){
enemies.Add(enemyAi);
}
}
RaycastHit hit;
Ray shooterRay = new Ray(transform.position, transform.forward);
if(Input.GetButton("Fire1") && canFire && clipAmmo > 0){
StartCoroutine (SoundCooldown(1));
foreach(EnemyAi enemy in enemies){
EnemyAi enemyAi = enemy.GetComponent<EnemyAi>();
enemyAi.couldHearGun = true;
}
}
if(Input.GetButton("Fire1") && canFire && clipAmmo > 0){
Debug.DrawRay (transform.position, transform.forward, Color.green);
if(Physics.Raycast(shooterRay, out hit, Mathf.Infinity)){
if(hit.collider.tag == "Enemy"){
hit.transform.GetComponent<EnemyCC>().health -= 15;
}
}
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(decal, hit.point, hitRotation);
canFire = false;
clipAmmo -= 1;
missingShots += 1;
StartCoroutine(FireRate(1));
}
if(Input.GetButtonDown("Fire1") && canFire && clipAmmo == 0 && !reloading){
reloading = true;
StartCoroutine(ReloadTime(3));
}
if(Input.GetButtonDown("Reload") && storedAmmo != 8 && !reloading){
reloading = true;
StartCoroutine(ReloadTime(3));
}
}
public IEnumerator FireRate (float delay){
yield return new WaitForSeconds(delay);
canFire = true;
}
public IEnumerator SoundCooldown(float delay){
EnemyAi enemyAi = enemy.GetComponent<EnemyAi>();
yield return new WaitForSeconds(delay);
enemyAi.couldHearGun = false;
}
public IEnumerator ReloadTime (float delay){
yield return new WaitForSeconds(delay);
reloading = false;
if(storedAmmo >= 8){
storedAmmo -= missingShots;
clipAmmo += missingShots;
missingShots = 0;
}
}
}
Answer by theLittleSettler · Jul 22, 2014 at 12:59 PM
Ok, you have a variable enemy of type GameObject as well as another variable enemy of type EnemyAi in the foreach loop. That's fine in c#, but it won't help monodevelop's intellisence.
foreach(EnemyAi enemy in enemies) {
// this line makes no sense because enemy is type EnemyAi
// though this.enemy is type GameObject, so it compiles
EnemyAi enemyAi = enemy.GetComponent<EnemyAi>();
enemyAi.couldHearGun = true;
}
You meant...
foreach(EnemyAi enemyAi in enemies) {
enemyAi.couldHearGun = true;
}
Keep an eye out for naming conflicts as they tend to cause more confusing bugs.
thanks so much, both of you but this is what worked for me
Answer by Paprik · Jul 22, 2014 at 12:42 PM
You probably didn't remove the enemy object from your list, so you keep cycling through it:
foreach(GameObject enemyObj in enemyObjects)
When destroying the enemy, you should remove it from enemyObjects as well. Perhaps in the OnDestroy method or wherever you kill it.
right, that would make sense. silly question but how do i remove it? i really dont get how lists work tbh. i just added this to the enemies script but it dosnt work:
if(health <= 0){
PistolRay pistolRay = pistol.GetComponent<PistolRay>();
pistolRay.enemies.Remove(pistolRay.enemyAi);
Destroy(gameObject);
}
}