Bullet Spawn

Hi!
Currently, I’m making an easy FPS game
Here are codes I’ve written:

    void Start () {
	StartCoroutine (BulletRespawn());
}

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

IEnumerator BulletRespawn()
{
	if(Input.GetMouseButton(0)) {
		yield return new WaitForSeconds (0.1f);

		Instantiate (bullet, respawnTr.position, Quaternion.identity);
	}
}

And

public GameObject bullet;
public float bulletSpeed = 50.0f;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	bullet.transform.Translate (0, -bulletSpeed * Time.deltaTime, 0);
    }

And

public GameObject ChrCamera;

//public float mouseX;
//public float mouseZ;

public float chrSpeed = 10.0f;

// Use this for initialization
void Start () {
	//Cursor.visible = false;
	//Cursor.lockState = CursorLockMode.None;
}

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

	//mouseX = Input.mousePosition.x;
	//mouseZ = Input.mousePosition.z;

	if (Input.GetKey (KeyCode.W)) {
		ChrCamera.transform.Translate (0, 0, chrSpeed * Time.deltaTime);
	}

	if (Input.GetKey (KeyCode.S)) {	
		ChrCamera.transform.Translate (0, 0, -chrSpeed * Time.deltaTime);
	}

	if (Input.GetKey (KeyCode.A)) {
		ChrCamera.transform.Translate (-chrSpeed * Time.deltaTime, 0, 0);
	}

	if (Input.GetKey (KeyCode.D)) {
		ChrCamera.transform.Translate (chrSpeed * Time.deltaTime, 0, 0);
	}
}

However, when I run the program, it crushes
How shoud I change the code???

What’s the error?

After reading your code I highly recommend you watch a few tutorials on basic c# scripting within unity and watch a few on the particular subject of firing bullets.

Having said that… it would be nice to know what your error log is when you try to run this script.

I can see a few things that are probably not good ways to go about this sort of thing…
I would suggest that you get rid of the whole co-routine concept and opt of a much cleaner and more efficient route.
First issue I see with your code is that there is no reference to the bullet you are trying to instantiate in your first script… second thing is you should be checking for input down and input ups in an update method, this way on the input down you can start to InvokeRepeating() on a Shoot() method, inversely on input up you can cancel the invoke… this is a much much more sensible way to do what you are trying to do.

Even if you do get this code working I highly recommend that you start studying as much as possible and when you get stuck with something look for a tutorial so you can see how other people achieve things, very powerful way to learn, if you try to do everything from scratch yourself you will still learn but you will form a lot of bad habits that will result in messy, buggy and un-reusable code.

I am assuming the problem lies in your first script as you didn’t handle the couroutine properly. I don’t see why you’d need to use couroutines for this anyways. We can fix this and check if the input is received per frame. Try this:

void Update()
{
    if (Input.GetButton("Fire");)
    {
        Instantiate(bullet, respawnTr.position, Quaternion.identity);
    }
}

Also, you can get rid of the Start() function. I also changed the input line as you were calling for a float and not a string. The Input.GetButton() function takes a string as a parameter. I titled it for you, so you’d need to go to Edit > Project Settings > Input and create a field titled “Fire” to handle this.