- Home /
C# How to have a gun select
Just wanted to know how i can have a gun select spawn the correct gun when i pick it in my loadout. what i have so far is:
if (GUILayout.Button ("Change Guns"))
{
if (GUILayout.Button ("Pistol"))
{
if (GUILayout.Button ("Test Pistol"))
{
}
}
if (GUILayout.Button ("LMG"))
{
}
if (GUILayout.Button ("Assault Rifle"))
{
}
}
just saying, i know most of this is wrong, i changed it but still don't know how to have a gun select
Answer by aldonaletto · Jul 25, 2013 at 06:12 PM
A simple way is to have all weapons childed to an empty object which in turn is childed to the camera, and deactivate all of them but the one currently selected - deactivated objects aren't rendered, neither their scripts are functional, thus you effectively has only one weapon on your hands at any time. Take a look at this answer, which is based on the good FPS Tutorial.
so i have them all child of the camera and all of them invisible and when i press spawn i spawn another one of the weapon i picked and make it visible?
$$anonymous$$ore or less: you have all weapons attached to the camera, but only one of them is active. Picking a weapon actually is a fake operation: you just set some variable to indicate that the weapon is available - take a look at this answer.
@aldonaletto Thanks man, this seems like it would be more efficient than the way I have been doing it all this time using instantiations. I learn something new every day here, +1 :D
Your answer
Follow this Question
Related Questions
How do I send a simple Vector3 over a network? 1 Answer
C# How to make the gun remember who equipped it 1 Answer
Syncing localScale 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
C# How to have weapon pickup 1 Answer