Hi, i recently updated unity from about 3.2 to 3.4 and I just started working on a game i had taken a break from. alot of stuff is broken now. anyway one problem i am having is my ship(player) is not firing bullets.
please note: this and all my code worked perfectly fine when i took a break 3 months ago
through the use of monodevelop debugging and debug.log’s i have tracked the problem to
InvokeRepeating("spawnBullet", firstShotDelay, fireRate);
when this function is executed this class is supposed to repeat
void spawnBullet() {
Instantiate(bullet,transform.position,transform.rotation);
}
however through debugging i have concluded that InvokeRepeating is either being executed and not working, or is being skipped over, however through breakpoints i have watched it execute and do nothing.
MY QUESTION: if anyone knows why i am having these problems please help me out. why is InvokeRepeating not working? what did unity update do to my game?
here is the code for spawning a bullet in my game which i am having this problem, i have some comments in please ask any questions if it is confusing:
using UnityEngine;
using System.Collections;
public class bulletspawner : MonoBehaviour {
public GameObject bullet;
public GameObject spreadShotBullet;
public GameObject rapidFireBullet;
public float firstShotDelay = 0.02f;
public float fireRate = 0.2f;
public float rapidFireRate = 0.1f;
public bool isSpreadShot = false;
private bool spreadShotEnabled = false;
private bool rapidFireEnabled = false;
private bool needsUpdate = false;
public static bool rapidFire = false;
public static bool spreadShot = false;
//spawns a bullet at the ships position and rotation
void spawnBullet() {
Instantiate(bullet,transform.position,transform.rotation);
}
void spawnSpreadBullet() {
Instantiate(spreadShotBullet,transform.position,transform.rotation);
}
void spawnRapidFireBullet() {
Instantiate(rapidFireBullet,transform.position,transform.rotation);
}
void spawnTheBullets ()
{
//this repeats spawning the bullet at a firerate in seconds
if (!isSpreadShot && !spreadShotEnabled && !rapidFireEnabled)
{
InvokeRepeating("spawnBullet", firstShotDelay, fireRate);
}
else
{
//Debug.Log("ss:" + spreadShotEnabled + " rf:" + rapidFireEnabled);
if (spreadShotEnabled && !rapidFireEnabled)
{
InvokeRepeating("spawnSpreadBullet", firstShotDelay, fireRate);
}
else
{
CancelInvoke("spawnSpreadBullet");
}
if (rapidFireEnabled && !isSpreadShot && !spreadShotEnabled)
{
InvokeRepeating("spawnRapidFireBullet", firstShotDelay, rapidFireRate);
}
else
{
CancelInvoke("spawnRapidFireBullet");
}
}
}
void stopSpawning()
{
CancelInvoke("spawnBullet");
CancelInvoke("spawnSpreadBullet");
CancelInvoke("spawnRapidFireBullet");
}
void Awake (){
spreadShotEnabled = false;
rapidFireEnabled = false;
rapidFire = false;
spreadShot = false;
if (globalController.overide)
{
fireRate = globalController.gnormalFireRate;
rapidFireRate = globalController.grapidFireRate;
}
}
// Update is called once per frame
void Update () {
if (rapidFireEnabled != rapidFire || spreadShotEnabled != spreadShot)
{
stopSpawning();
needsUpdate = true;
}
rapidFireEnabled = rapidFire;
spreadShotEnabled = spreadShot;
if (needsUpdate && Input.GetButton("Fire1"))
{
needsUpdate = false;
spawnTheBullets();
}
//spreadShotEnabled = shipcontrols.globeSpreadShotEnabled;
//Debug.Log(spreadShotEnabled);
//the frame the fire1 input is pressed it starts a repeating function
//note: that if this is not done multiple instances of a bullet repeatedly spawning will occur.
// as the repeating will be invoked every frame.
if(Input.GetButtonDown("Fire1")){
spawnTheBullets();
}
//the frame the button is released the repeated spawning of the bullet is stopped
if(Input.GetButtonUp("Fire1")){
stopSpawning();
}
}
}