- Home /
Weapon Switching and Inventory
I have a game that you can carry a certain amount of items in your inventory depending on your strength and I just would like some suggestions on how i might go about this. It would also be great if anyone could help me with weapon switching also.
It's advisable you ask only one question in each post. $$anonymous$$aybe you can limit your question to contain just one (edit it) and create a new question for your weapon switching - or even use the search function. I bet it's been asked plenty of times before.
Agreed. Look at my page by clicking on my name at the end of the comment and go to my "Gun manager not working" question. it doesn't have any answers, but that was because I was an idiot. That will help with the weapon switching. All you will need in addition is a gun script that has a activation keycode variable.
Answer by EytanTKing · Mar 20, 2011 at 08:02 PM
weapon switching
Inside your player create an empty Game Object and attach this Javascript to it:
function Start () { SelectWeapon(0); }
function Update () { if (Input.GetButton ("Fire1")) BroadcastMessage("Fire");
if (Input.GetKeyDown("1")) { SelectWeapon(0); }
else if (Input.GetKeyDown("2")) { SelectWeapon(1); } else if (Input.GetKeyDown("3")) { SelectWeapon(2); } else if (Input.GetKeyDown("4")) { SelectWeapon(3); }
}
function SelectWeapon (index : int) { for (var i=0;i
Now put the all the player weapons inside (children of) the empty Game Object,
if you Want to add more weapons insert:
else if (Input.GetKeyDown("Key to Press")) { SelectWeapon(Weapon Number); }
Replace Key to Press with any key that you want to be pressed to change to that weapon,
Replace Weapon Number with the child number (starting from the top [0,1,2 etc.]).
I hope that was helpful!<-:
Your answer
![](https://koobas.hobune.stream/wayback/20220613113837im_/https://answers.unity.com/themes/thub/images/avi.jpg)