Ability to shoot won't work

In a chapter in this book I’m using to learn Unity, it tells me a script to type into the Player Controller script to make the spaceship fire when I click. I did it exactly as it says in the book, but for some reason, the ship won’t fire. Here’s the code:

public class PlayerController : MonoBehaviour
{
private Rigidbody ThisBody = null;

public bool MouseLook = true;
public string HorzAxis = “Horizontal”;
public string VertAxis = “Vertical”;
public string FireAxis = “Fire1”;
public float MaxSpeed = 5f;

public float ReloadDelay = 0.3f;
public bool CanFire = true;

public Transform[ ] TurretTransforms;

void Awake()
{
ThisBody = GetComponent();
}

void FixedUpdate()
{
float Horz = Input.GetAxis(HorzAxis);
float Vert = Input.GetAxis(VertAxis);
Vector3 MoveDirection = new Vector3(Horz, 0.0f, Vert);
ThisBody.AddForce(MoveDirection.normalized * MaxSpeed);
ThisBody.velocity = new Vector3
(Mathf.Clamp(ThisBody.velocity.x, -MaxSpeed, MaxSpeed),
Mathf.Clamp(ThisBody.velocity.y, -MaxSpeed, MaxSpeed),
Mathf.Clamp(ThisBody.velocity.z, -MaxSpeed, MaxSpeed));

if (MouseLook)
{
Vector3 MousePosWorld = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f));
MousePosWorld = new Vector3(MousePosWorld.x, 0.0f, MousePosWorld.z);
Vector3 LookDirection = MousePosWorld - transform.position;

transform.localRotation = Quaternion.LookRotation(LookDirection.normalized, Vector3.up);

}

if(Input.GetButtonDown(FireAxis) && CanFire)
{
foreach(Transform T in TurretTransforms)
{
AmmoManager.SpawnAmmo(T.position, T.rotation);
}

CanFire = false;
Invoke(“EnableFire”, ReloadDelay);
}
}

void EnableFire()
{
CanFire = true;
}

Anyone know how to fix this? Because I checked all my scripts and they’re accurate to what the book says to do.

In the future please paste your code into the forums with this button for proper formatting:

If your book told you to put Input.GetButtonDown(FireAxis) inside FixedUpdate, I would start searching for a better book.

That particular code will not work reliably inside FixedUpdate. That’s because FixedUpdate does not run every frame, and GetButtonDown is only true during the first frame in which the button is pressed. It is possible and likely for FixedUpdate to miss that frame.

Now I’m not 100% sure that’s your only problem but that’s what jumped out at me first.

The other thing to check is - what are all your values set to in the inspector? Do you have any transforms assigned to TurretTransforms for example? If not, nothing will fire as well.

Lastly - make sure to check your console window for any errors at runtime.

Console window says no errors. Moving the fixed update code to the update section didn’t work. And I have Turret Transform set to 1.

You have it set to 1? What do you mean? Did you assign any transforms to it?

This is likely a combination of code and/or scene setup.

To help you arrive at more useful observations than “won’t fire” and “didn’t work,” as well as for you to generally gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

You could also just display various important quantities in UI Text elements to watch them change as you playtest.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

The number right next to the label. And yes, there is a transform assigned to it.
7131776--852356--FC389C0A-370F-462B-A81E-611620313A92.png

AH-HA! It was where the Ammo Manager was in the scene that was causing the issue. Thank you.