- Home /
Weapon Switching Help
Hey im making a fps and i have all of my weapons and i have an in game pause menu to where you can buy weapons in a store. Im new to using child/parenting functions so im not sure on where to start with my coding. What i am trying to do is when a var is true it selects a certain child of the weapons game object and deselects the other when you press either 1 or 2, 1 being the primary weapon and the 2 being the secondary. Im trying to use this code but its not working how i want it to.
static var beretta = true;
static var deagle = false;
static var revolver = false;
static var m3 = false;
static var mp5 = false;
static var mp7 = true;
static var spaz = false;
static var trench = false;
static var toyshotgun = false;
var weapon : int;
function Start ()
{
weapon = 0;
}
function Update ()
{
if (Input.GetKeyDown("1"))
{
if (m3)
weapon = 1;
if (mp5)
weapon = 2;
if (mp7)
weapon = 3;
if (spaz)
weapon = 4;
if (trench)
weapon = 5;
if (toyshotgun)
weapon = 6;
}
if (Input.GetKeyDown("2"))
{
if (beretta)
weapon = 0;//
if (deagle)
weapon = 7;
if (revolver)
weapon = 8;
}
}
function SelectWeapon (index : int)
{
for (var i=0;i<transform.childCount;i++)
{
if (i == index)
transform.GetChild(weapon).gameObject.SetActiveRecursively(true);
else
transform.GetChild(weapon).gameObject.SetActiveRecursively(false);
}
}
Unity Answers ate your code after the < character in SelectWeapon - it "thinks" this character is a tag and skip all the text ahead until a closing > is found. Edit your question and post the code again - but this time skip a blank line after the question text, select the code and press ctrl-$$anonymous$$ to format it
Answer by sparkzbarca · Oct 30, 2012 at 05:27 AM
is every weapon a child of the player, even ones they dont own? thats wierd. That is by the way what your code assumes.
for (var i=0;i<transform.childCount;i++)
if they own 1 weapon the childcount might only be equal to 1. meaning that for loop will stop after the first thing.
change the code up
if(m3)
getcomponent<M3>();
//do what you want with the M3
if(berreta)
getcomponent<berreta>();
//do what you want with the berreta
each gun should have a script each script should be the name of the gun
your probably thinking, but i want to call fire on all these weapons but have fire do different things depending on the weapon. That means you want a base class called weapon and then subclasses that inherit from it and override the base methods.
for example
class weapon()
{
public
bool fire();
}
bool weapon.fire(){
//do nothing now, your just creating a base so you can override later
return true;
}
class weapon::M3()
{
public
bool fire();
}
bool weapon::M3 fire()
{
fire as is appropriate for an M3
}
you do that for each weapon
now it is both a weapon and an M3 (polymorphism)
now you can create a weapon and call fire and it will do different things depending on the type of weapon it is.
yes i do have them all as a child of a GameObject called weapons which is a GameObject of the main camera. What i am asking is how do i enable a certain child and disable all the other children if a variable is true?
there not a bool are they? what do you mean enable and disable, they are game items. You can for example create a weapons variable and change which weapon is assigned to it.
What does enabling mean in context? it means you send commands to that specific weapon? well create an empty weapon variable and assign weapons to the variable and do things with the variable.
Variables are so things can vary, you want a fire command on a weapon but you want the weapon to vary. you want a weapon class so you can make a weapon variable in your scrip.t
Your answer
![](https://koobas.hobune.stream/wayback/20220613083053im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Adding objects on player models in a multiplayer game 0 Answers
Swapping Weapons In Game 0 Answers
Getting into a veichle 2 Answers
switch weapons script not working? 2 Answers