- Home /
This question was
closed Dec 01, 2016 at 04:01 PM by
MegaDiscGames for the following reason:
The question is answered, right answer was accepted
Question by
MegaDiscGames · Nov 30, 2016 at 07:37 AM ·
c#2dspace shooter
Lasers wont stop firing??
Hello! I have been working on a 2d space shooter, but recently, I found an error Whenever I play the game, I dont need to press the b key, lasers fire regardless. Here is my code using UnityEngine; using System.Collections;
public class Firing : MonoBehaviour {
public GameObject laser;
public GameObject bulletpositiona;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey("b")) ;
{
GameObject bullet = (GameObject)Instantiate(laser);
bullet.transform.position = bulletpositiona.transform.position;
}
}
}
Thank you!!
Comment
Best Answer
Answer by slavo · Nov 30, 2016 at 04:20 PM
Hi you have semicolon after your If. SO what it does is If (Input.GetKey("b")) then do empty command. Do rest regardless of if. Just remove it and it will work fine.
public class Firing : MonoBehaviour {
public GameObject laser;
public GameObject bulletpositiona;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey("b")) //; Wrong semicolon
{
GameObject bullet = (GameObject)Instantiate(laser);
bullet.transform.position = bulletpositiona.transform.position;
}
}
}
Follow this Question
Related Questions
How to remove a specific prefab from a List? 1 Answer
2d bullet shot at bullet emitter 0 Answers
Fire limit in a Space shooter? 1 Answer
Evasive Maneuver to Space Shooter 2D. 1 Answer
Multiple Cars not working 1 Answer