- Home /
Start() not firing when switching guns
I currently have a ak47 weapons class for shooting, animations, etc I also have a mk11 sniper class for the same reasons.
I have a weapons switching script that seems to be buggy. In my weapons switching script i have an array for the guns. I start with no weapon and i can select 2 on the keyboard and the ak47 shows up and start fires() in the ak47 class then i can select key 4 for the sniper and start fires(). This is where it gets interesting. When i then select 2 the start does not fire but the gun(ak47) is selected. I can then fire the gun and everything updates. when the ak47 is selected and i hit 4 the sniper is loaded but the start is not fired. I even added a print() in the start to see when its being fired.
My hierarchy: FPS first person controller. parented to that is a blank object "WeaponCameraRoot" parented to that is my weapons camera. Parented to that is a blank object called "PlayerWeapons" its children are my weapons.
I have the weapons select script attached to the blank object "PlayerWeapons" that the parent of my weapons.
Here is my weapons select script
public class WeaponsSelect : MonoBehaviour {
public GameObject[] weapons;
public int selectedWeapon;
void Start () {
SelectWeapon(0);
selectedWeapon = 0;
}
void Update () {
if(Input.GetKeyDown("1") && selectedWeapon != 1){
if(selectedWeapon < 1)
{
SelectWeapon(0);
selectedWeapon = 1;
}
}
else if(Input.GetKeyDown("2")&& selectedWeapon != 2){
SelectWeapon(1);
selectedWeapon = 2;
}
else if(Input.GetKeyDown("3")&& selectedWeapon != 3){
SelectWeapon(2);
selectedWeapon = 3;
}
else if(Input.GetKeyDown("4")&& selectedWeapon != 4)
{
SelectWeapon(3);
selectedWeapon = 4;
}
}
private void SelectWeapon(int index)
{
foreach(GameObject obj in weapons)obj.SetActiveRecursively(false);
weapons[index].gameObject.SetActiveRecursively(true);
}
}
I dont think theres a need to show you all the ak47 class.
void Start () {
print ("start called");
anim = (AK47_animations)FindObjectOfType(typeof(AK47_animations));
bulletsLeft = bulletsPerClip;
audio.clip = load;
audio.Play();
Gui ();
// MuzzleFlash.enabled = false;
//muzzleLight.enabled = false;
}
For some reason it will let me switch out the guns but the start() in each class only works once..
What can i do?
Answer by whydoidoit · Sep 16, 2012 at 10:55 AM
You are better off using OnEnable rather than Start if the item will be switched on and off. OnEnable will be called every time the item is activated (you can use OnDisable for deactivation too).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Set default length for an array of elements of a custom class in inspector 0 Answers
Move files from folder a to folder b 1 Answer
Array index help. 1 Answer