- Home /
Shooting bullets with Instantiate function
Hey guys. I'm trying to shoot a bullet, and I somehow managed to do it. But now I added a shop script, and it doesn't shoot anything. Do you see anything wrong? The script itself doesn't have an error.
public class BulletGenerator : MonoBehaviour { public GameObject BulletPrefab; public static BulletGenerator instance; private ShopScript shop; private List stat = new List(); public int capacity = 15; public int mags = 12; public float FireRate = 10; public float lastfired; void Awake() { if (instance == null) { instance = this; }
}
void Start () {
shop = ShopScript.instance;
stat = shop.enabled;
}
// Update is called once per frame
void Update ()
{
NewMethod();
if (stat[1] == true) {
mags += 7;
stat[1] = false;
stat[1] = shop.enabled[1];
}
}
private void NewMethod()
{
if (stat[6] == true)
{
if (Input.GetMouseButton(0))
{
if (Time.time - lastfired > 1 / FireRate)
{
lastfired = Time.time;
shootFunction();
}
}
}
else
{
if (Input.GetMouseButtonDown(0))
{
shootFunction();
}
}
}
IEnumerator shootFunction() {
if (Input.GetMouseButtonDown(0))
{
if (capacity < 1)
{
yield return new WaitForSeconds(2f);
if (stat[2] == true)
{
capacity = 30;
}
else {
capacity = 15;
}
mags -= 1;
}
else {
if (mags < 0)
{
}
else {
Instantiate(BulletPrefab, transform.position, transform.rotation);
capacity -= 1;
}
}
}
}
}
Hi @CalebSong - You code is not formatted properly, first lines are outside code formatting...
What is the condition "stat[6] == true"?
Asking because else part looks a bit strange.
You are calling coroutine like it is a method - "shootFunction();" ins$$anonymous$$d of using StartCoroutine.
You are querying input the second time in your "shootFunction" coroutine?
In your coroutine, you are once again doing some "stat[2] == true" check? only in else part you are shooting something...
Your answer
Follow this Question
Related Questions
How Can I Make a Projectile Move? 2 Answers
Issue with instantiation delay that pooling doesn't fix 0 Answers
How can I fire bullets at mouse position 1 Answer
Power-Ups increasing projectile stats 1 Answer
Projectiles too fast for Physics 3 Answers