# Fire at enemy script doesnt work [closed]

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;
transform.LookAt(pos);

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

}

if (move)
{
transform.position = Vector3.MoveTowards(transform.position, character.position, enemySpeed * Time.deltaTime);
transform.LookAt(pos);
}
if (shoot)
{
shootAt();
}

void shootAt(){

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

}

}


* */

## 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.

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

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