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 * 



* */


closed as off-topic by Philipp, Tyyppi_77, Anko, DMGregory Dec 13 '18 at 15:11

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions about debugging a problem must provide a minimal, complete, verifiable example of the issue so that readers can diagnose it without needing to guess, read all of your code, or engage in extensive back-and-forth dialog. For more information, see this meta thread." – Anko, DMGregory
  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Philipp, Tyyppi_77
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Prometheus Dec 5 '18 at 23:15
  • \$\begingroup\$ distance = Vector3.Distance(transform.position, character.position); here i declared. i get no erors \$\endgroup\$ – user245548 Dec 6 '18 at 0:00
  • \$\begingroup\$ 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? \$\endgroup\$ – DMGregory Dec 6 '18 at 1:05
  • 1
    \$\begingroup\$ no, i edited because it didnt solve my problem \$\endgroup\$ – user245548 Dec 6 '18 at 18:33
  • 1
    \$\begingroup\$ maybe you can look at this script and give your solution \$\endgroup\$ – user245548 Dec 6 '18 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.

  • \$\begingroup\$ now it says variable hasnt been assigned \$\endgroup\$ – user245548 Dec 6 '18 at 18:02
  • \$\begingroup\$ variable character hasnot been assigned \$\endgroup\$ – user245548 Dec 6 '18 at 18:21
  • \$\begingroup\$ @user245548 It should also tell you which variable at which line. \$\endgroup\$ – Philipp Dec 6 '18 at 18:22
  • \$\begingroup\$ You should probably need to assign character value of AntagonisticElcreator \$\endgroup\$ – user245548 Dec 6 '18 at 18:22
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Philipp Dec 6 '18 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)


  • \$\begingroup\$ it syas keyword void cannot be used in ths cotext \$\endgroup\$ – user245548 Dec 6 '18 at 11:10
  • \$\begingroup\$ 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. \$\endgroup\$ – Nikos Giannaras Dec 6 '18 at 11:59
  • \$\begingroup\$ it says variable character hasnot been assigned \$\endgroup\$ – user245548 Dec 6 '18 at 18:03
  • \$\begingroup\$ @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. \$\endgroup\$ – DMGregory Dec 12 '18 at 11:45

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