- Home /
need help with instantiating for making a bullet shoot
trying to make it so when i hit "D" in game it shoots a bullet. I keep getting an error that says "An embedded statement may not be a declaration or labeled statement" here is my script:
using UnityEngine; using System.Collections;
public class Creator : MonoBehaviour {
public Rigidbody ThePrefab;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.D))
Rigidbody instance;
instance = Instantiate(thePrefab,transform.position, transform.rotation);
}
}
Answer by perchik · Aug 26, 2013 at 05:50 PM
Surround your if
statement with curly braces:
if(Input.GetKey(KeyCode.D))
{
Rigidbody instance;
instance = Instantiate(thePrefab,transform.position, transform.rotation);
}
Also, just so you don't think I'm magic or anything, here's how I solved this problem:
I googled the exact text of the error "An embedded statement may not be a declaration or labeled statement"
The top hit was from a .Net forums page. Someone else had the same problem.
The answer in the forum was "if you have to declare something in an if statement, put it in curly braces"
You're magic anyway lol +1 for a good answer and explanation :D