I need my enemies to fire at me. I have a script and I think all is correct. I couldnt figure out why it doesnt work?

public class AntagonisticElCreator : MonoBehaviour {

public GameObject boom;
public float enemySpeed;

public bool move=false;
public bool shoot=false;
public Transform character;
Vector3 pos;
int boomnumber;

 public float bulletSpeed;
public float bulletDestroyTime;

public GameObject bulletPrefab;
public Transform bulletSpawn;

void Start () {
   bulletSpeed = 15f;
bulletDestroyTime = 8f;
    NumbarofBullet = 105;

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

    pos = new Vector3(character.position.x, transform.position.y, character.position.z);
    distance = Vector3.Distance(transform.position, character.position);
    if (distance < 20){
        move = true;
        shoot = false;

    if (distance < 10)
        move = false;
        shoot = true;


    if (move)
        transform.position = Vector3.MoveTowards(transform.position, character.position, enemySpeed * Time.deltaTime);
    if (shoot)

        void shootAt(){

        Instantiate(explsn, bulletSpawn.transform.position, 
      var bullet = Instantiate(bulletPrefab, bulletSpawn.position, 
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 



* */

  • 1
    Do you get any errors? Use Debug.Log for the distance transform, to se if it is firing: Debug.Log(distance). Also, where did you declared distance? Because in the script youve posted, there isnt a variable called distance. – Prometheus Dec 5 at 23:15
  • distance = Vector3.Distance(transform.position, character.position); here i declared. i get no erors – user245548 Dec 6 at 0:00
  • That's an assignment, not a declaration. A declaration in C# would look like float distance or var diatance. How does the current behaviour of your script differ from what you want? – DMGregory Dec 6 at 1:05
  • 1
    no, i edited because it didnt solve my problem – user245548 Dec 6 at 18:33
  • 1
    maybe you can look at this script and give your solution – user245548 Dec 6 at 18:34

You are trying to define a new method within the code of another method. C# does not allow you to do that until version 7.0 (which the current Unity version 2018.2 doesn't support yet).

Move the definition of the void ShootAt method out of the void Update method so it's a private method within the class AntagonisticElCreator.

  • now it says variable hasnt been assigned – user245548 Dec 6 at 18:02
  • variable character hasnot been assigned – user245548 Dec 6 at 18:21
  • @user245548 It should also tell you which variable at which line. – Philipp Dec 6 at 18:22
  • You should probably need to assign character value of AntagonisticElcreator – user245548 Dec 6 at 18:22
  • 2
    @user245548 character is a public variable, so it is likely supposed to be set in the inspector in the Unity editor by dragging a specific game object into it. – Philipp Dec 6 at 18:28

i can see the following

  1. Is this the whole script? And if so is it getting any errors? Cause i see for example that distance is not declared and the shootAt() function is inside Update().
  2. Declare your distance variable.
  3. Try to make the initializations in the Start() and not in the Class body. ( example. public bool move=false; )
  4. In your Update(), i guess pos is the position of the character. Please check your Y axis input here ( you get x,z from character but y from enemy)


  • it syas keyword void cannot be used in ths cotext – user245548 Dec 6 at 11:10
  • This is because you have declared a function, inside another function which is wrong. Write code in a proper code editor in order to identify your errors. – Nikos Giannaras Dec 6 at 11:59
  • it says variable character hasnot been assigned – user245548 Dec 6 at 18:03
  • @user245548 then assign it. Select the script in the Inspector and click the little circle next to the "Character" field to select the transform in your scene that this variable should refer to. – DMGregory 11 hours ago

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.