Duplicated enemy won't shoot C#

Hi guys,

I am a bit new to scripting, but I am trying the best I can with the little knowledge I got. I am making a simple tank shooting game in C#. Just when I thought I was done with all the mechanics I am encountering a new problem.

I only have one enemy tank in the scene, and it is able to look at the Player position and shoot at it whenever the player gets in “range” (I am using triggers for this. But once I duplicate the original enemy tank, only the new one would detect the player on it’s range. unless the duplicated enemy tank detects it.

This is the BulletSpawnerScript I created for the enemy. It is attached to a “bulletSpawner” gameObject on the tip of the cannon, and checks for another script that is attach to a trigger (Range).

I would appreciate if someone can tell me what am I doing wrong and what can I do to fix it. In case my English is not good enough, I am also attaching an image. Thanks

public class EnemyBulletSpawnScript : MonoBehaviour {

public Rigidbody bullets;
public float bulletSpeed = 50;
public AudioClip ShootSound;
public bool enemyFireNow = false;
// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{
    

    if (GameObject.Find("ShootingRange").GetComponent <ShootingRange> ().fireNow) 
	{
        enemyFireNow = true;
        GetComponent<AudioSource>().PlayOneShot(ShootSound);
        Rigidbody spawnedBullet = Instantiate(bullets, transform.position, transform.rotation) as Rigidbody;
		spawnedBullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
	}
    if (GameObject.Find("InnerShootingRange").GetComponent <ShootingRange> ().fireNow) 
	{
        enemyFireNow = true;
        GetComponent<AudioSource>().PlayOneShot(ShootSound);
        Rigidbody spawnedBullet = Instantiate(bullets, transform.position, transform.rotation) as Rigidbody;
		spawnedBullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
	}

    if (GameObject.Find("CloseShootingRange").GetComponent<ShootingRange>().fireNow)
    {
        enemyFireNow = true;
        GetComponent<AudioSource>().PlayOneShot(ShootSound);
        Rigidbody spawnedBullet = Instantiate(bullets, transform.position, transform.rotation) as Rigidbody;
        spawnedBullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
    }
    else
    {
        enemyFireNow = false;
    }

}

}

Can you add a screenshot of your Unity Hierachy of GameObjects?

Correct me if I’m wrong but my understanding is:

.Tank1

… ShootingRange

… InnerShootingRange

… CloseShootingRange

… bulletSpawner

. Tank2

… ShootingRange

… InnerShootingRange

… CloseShootingRange

… bulletSpawner

Each tank has it’s own shooting range objects and bulletSpawner correct?

BTW the following calls in Update are not efficient. I’m thinking it is also the cause of the errors. It’s checking for the first, I believe, ShootingRange,etc object that it can find (doesn’t matter which tank) and using that one. That’s why your one tank isn’t shooting unless the other does.

GameObject.Find(“ShootingRange”).GetComponent ()
GameObject.Find(“InnerShootingRange”).GetComponent ()
GameObject.Find(“CloseShootingRange”).GetComponent()

To fix this:

In the start method, you should .Find(“ShootingRange”) etc and save it to a variable then in the update use that variable.

public class EnemyBulletSpawnScript : MonoBehaviour {
private ShootingRange shootingRange;
private ShootingRange innerShootingRange;
private ShootingRange closeShootingRange;

void Start()
{
  Transform parent = gameObject.transform.parent;
  shootingRange = (parent.Find("ShootingRange").gameObject).GetComponent<ShootingRange>();
  innerShootingRange = (parent.Find("InnerShootingRange").gameObject).GetComponent<ShootingRange>();
  closeShootingRange = (parent.Find("CloseShootingRange").gameObject).GetComponent<ShootingRange>();
}

void Update()
{
  if (shootingRange.fireNow) {
    ..
  }
  if (innerShootingRange.fireNow) {
    ..
  }
  if (closeShootingRange.fireNow) {
    ..
  }
}
}

I think this will do the trick. Gotta eat lunch now, I’ll edit this again after if I see any problems with it.