Group Group Group Group Group Group Group Group Group

Chapter 3, my SpaceMarine is shooting continuously

#1

Hi !
I bought Unity Games By Tutorial Second Edition, and I started it yesterday. I reached Chapter 3, and i finished it. I tested the game, and i met a problem : when i click with my mouse, just one time, my SpaceMarine shoots continuously, and i can’t make it stop. If i click again, there’s nothing happening. I verified the Gun script but it’s the same than in the book. How can i do ?

#2

Hey there, that sounds like a problem for sure. What is happening is that when you press the mouse button, you’re invoking a repeating method. It won’t stop until you tell it to stop. The way to make it stop is to release the mouse button.

You should have some code that looks this:

if (Input.GetMouseButtonUp(0)) 
{
    CancelInvoke("fireBullet");
}

My guess is that you have a mispelling or a typo. For instance, if you call CancelInvoke with a FireBullet instead of fireBullet, then the script won’t stop firing. Or you could have done: GetMouseButtonUp(1) instead of GetMouseButtonUp(0). GetMouseButtonUp(1) would listen to right click instead of your left click.

Definitely review your code again and compare it to the book text. If you can’t find the error, then paste the contents of Gun.cs here and I’ll take a closer look.

Cheers!
Brian

#3

Thanks for that more-than-rapid answer ! I’m gonna check again, and if i don’t find anything i’ll send you my Gun.cs script !

1 Like
#4

I didn’t find anything wrong… There’s my Gun.cs script for you :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour {
public GameObject bulletPrefab;
public Transform launchPosition;

void fireBullet() {
// 1
GameObject bullet = Instantiate(bulletPrefab) as GameObject;
// 2
bullet.transform.position = launchPosition.position;
// 3
	bullet.GetComponent<Rigidbody>().velocity =
		transform.parent.forward * 100;
}
// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update () {
	if (Input.GetMouseButtonDown (0)) {
		if (!IsInvoking ("fireBullet")) {
			InvokeRepeating ("fireBullet", 0f, 0.1f);
			if (Input.GetMouseButtonUp (0)) {
				CancelInvoke("fireBullet");
			}
		}
	}
}

}

#5

It appears that you put the Input.GetMouseButtonUp in the block for GetMouseButtonDown(). Effectively, that code will only fire when your press the button down (which of course, won’t happen because you’re releasing the mouse button.

To fix this issue, you need to put the GetMouseButtonUp if block underneath the GetMouseButtonDown block like so:

Update() {
    if (Input.GetMouseButtonDown (0)) {
        if (!IsInvoking ("fireBullet")) {
	        InvokeRepeating ("fireBullet", 0f, 0.1f);
        }
    }
    if (Input.GetMouseButtonUp (0)) {
        CancelInvoke("fireBullet");
    }
}

Cheers!

#6

Ooooh serious… How could i make such an error !? Thanks a lot ! I’m going to continue your book as soon as possible cause i like it. Good job !
Cheers !

#7

It’s okay - making errors is how we all become better developers. :slight_smile: If you get stuck anywhere else, feel free to reach out to me at any time.

#8

Hello !
I have another problem…
In the chapter 5. My GameManager script has errors, but i don’t find them. I looked twice in the book but nothing appears to me… I think it may be an error in the braces place, but i don’t even know. Please help me. Here is my script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
public GameObject player;
public GameObject[] spawnPoints;
public GameObject alien;
public int maxAliensOnScreen;
public int totalAliens;
public float minSpawnTime;
public float maxSpawnTime;
public int aliensPerSpawn;
private int aliensOnScreen = 0;
private float generatedSpawnTime = 0;
private float currentSpawnTime = 0;

// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update () {
	currentSpawnTime += Time.deltaTime;
	if (currentSpawnTime > generatedSpawnTime) {
		currentSpawnTime = 0;
	}
	generatedSpawnTime = Random.Range (minSpawnTime, maxSpawnTime);
	if (aliensPerSpawn > 0 && aliensOnScreen < totalAliens) {
		List<int> previousSpawnLocations = new List<int> ();
	}
	if (aliensPerSpawn > spawnPoints.Length) {
		aliensPerSpawn = spawnPoints.Length - 1;
	}
	aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn
		- totalAliens : aliensPerSpawn;
	for (int i = 0; i < aliensPerSpawn; i++) {
		if (aliensOnScreen < maxAliensOnScreen) {
			aliensOnScreen += 1;
			// code goes here
			// 1
			int spawnPoint = -1;
			// 2
			while (spawnPoint == -1) {
				// 3
				int randomNumber = Random.Range(0, spawnPoints.Length - 1);
				// 4
				if (!previousSpawnLocations.Contains(randomNumber)) {
					previousSpawnLocations.Add(randomNumber);
					spawnPoint = randomNumber;
				}
			}
			GameObject spawnLocation = spawnPoints[spawnPoint];
			GameObject newAlien = Instantiate(alien) as GameObject;
			newAlien.transform.position = spawnLocation.transform.position;
		}
	}
}

}

#9

Hey Overdoze,

Basically, the issue is with this line:

    if (aliensPerSpawn > 0 && aliensOnScreen < totalAliens) {
        List<int> previousSpawnLocations = new List<int>();
    }

You define previousSpawnLocations, but after the if-block, it goes out of scope. That’s why there’s a previousSpawnLocation doesn't exist error. Basically, you need put everything inside that brace, so it should look like this:

    if (aliensPerSpawn > 0 && aliensOnScreen < totalAliens)
    {
        List<int> previousSpawnLocations = new List<int>();
        if (aliensPerSpawn > spawnPoints.Length)
        {
            aliensPerSpawn = spawnPoints.Length - 1;
        }
        aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn - totalAliens : aliensPerSpawn;
        // rest of the code

Give it a shot and shoot me a message if you get stuck.

Cheers!
Brian

#10

Thanks !
Now, it works !

1 Like
#11

Hi ! Unfortunately, i have another problem… In chapter 6, “Animations”, all my animations work except one, the one of the alien. When i click the play button, the walk animation of the aliens works one time, and then stop. I don’t know how to fix this…
Please help. Thanks.

#12

Anyone can answer me ?

#13

@bdmoakley please help

#14

Hi @overdoze, Sorry for the delay. I was away over the weekend. Your animations probably aren’t repeating because they aren’t set to loop. Select the BobbleEnemy-Body in the Models folder and in the inspector, click on the Animation tab.

You should see a Loop Time checkbox. Make sure to select it, then click the Apply button. That will cause the animation to loop. Give it a shot and let me know how it turns out.

#15

I verified that, but the problem is not here, the box is checked and the apply button too. I don’t understand.

#16

So du u have any answer to my problem ?

#17

Does the animation loop in the inspector if you select the model and its walking animation?

#18

Hey there, my problem is still here and i’m waiting to solve it, i checked again but i don’t see anything… So in this post i put some screenshots to help you understand. Your help is welcome

#19

#20