Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Weapon Switching System

Hello, i want to improve my weapon switching system. I have followed brackeys tutorials and everything works completely fine. Now i want to add the feature that you can pick up weapons(i know that you can do that with colliders).

1. Problem: how do I make a weapon which i pick up a child of my parentObject (parentObject is the WeaponSwitcher which has the script WeaponSwitchingon it).

2. Problem: When i pick up different weapons they get a different order - so when you want to reference the script from another script for example you want to know if the knife is selected (which is currently my 3rd childObject) i can just say: if(selectedWeapon ==2) which works fine. But when they get in a different order it doesnt work so i think i should like call the childObjects by their name (sth. like: if(selectedWeaponString == "Knife") or anthing like that) or tag? Or is there maybe some other way or do i need to make a complete different system?

I appreciate if someone help me with this problem because it would improve my game a lot. Thanks :)


  • I am not an good programmer but i have an idea

    Every weapon will have a unique ID or nameID in order to find the weapon you will need to loop(for loop) through the child gameobjects....

    You just need to compare the name ID

    To the desired weapon Name(Name ID)

  • danieldaniel Member

    hey TumiTheSoleMaker thanks for your comment. i thought of somethig like this too. my problem is that i dont know how to call this "ID". i thought you could do that by just calling the name or tag of the childObject - but how do i do that in code and from other scripts?😬

  • edited May 2020

    In your weapons script create a new variable string maybe call it

    Public string weaponID=yourweaponsname

    And in a for loop just get this weaponId and compare it to your desired weaponId

    (Sorry currently not at home so i have write this all from my phone)

    For(int i=0;i<childgamobjects.lenth;i++){ if(childgamobjects[i].weaponId==desired weaponId){

    //do what every you want to do with it

    Return or something?


  • danieldaniel Member

    before, i didnt know what a for loop does but after some research i think i get it now. i have still got aquestion. is "childgameobjects" an array or sth else?

  • It can be an array or a list depends on you and your needs

  • danieldaniel Member

    thank you so much TumiTheSoleMaker😂(i didnt use the for loop but the research about it was intresting and also helped me) ok i actually solved the problem! it was way much easier than i thought it would be. i created the "ID" (just a string in my weaponSwitching script) and in my weapons scripts i overwrote the string when they are selected(enabled). so for example in my knife script i set the string in my weaponSwitching script to "knife"(in the OnEnable() function). now i can just check if the string == "knife".

Sign In or Register to comment.