- Home /
Switching Weapons (C#)
I know the logic that I want to implement, but I don't know how to actually program it. What I want to do is have it so that if you hit one of the alpha buttons (1, 2, 3) the script will enable the corresponding object in an array. Then I want to disable ever other object in the array. This way the number of weapons in the inventory can be changed. It would be really helpful for an explanation of a script as well. Thanks!
this question answer system is for "problems" not "design of my code". Ask in the normal forum for this kinda question.
secondly, try to search for similar tutorials on this. From what I read, it looks to me like an inventory kinda setup. Search for that in Google or in Unity forums. I believe there are even free assets in the Assets store for inventory, but I am not sure.
Question, do you need to deactivate every object in the array? Is it possible to run this like a FS$$anonymous$$, simply cache the gameObject that is currently active and only disable that one?
Otherwise you can use a for or foreach loop to iterate over the entire array.
Answer by Lazdude17 · Jul 02, 2014 at 03:56 AM
Make your List in your inventory script
using System.Collections.Generic;
private List<GameObject> weaponsList;
Then assign the Weapons you have to it so that after you pick up a weapon it goes into your inventory.
//On pick up of sword
inventory.weaponsList.Add(//gameobject here like sword)
Then after you equip the weapon you want like "sword" set it to the variable "currentWeapon"
foreach(GameObject weapon in inventory.weaponsList)
{
if(gameObject.activeSelf)
{
gameObject.SetActive(false);
}
}
//Now that they're all off you turn yours back on
currentWeapon.SetActive(true);
Now I know this is kinda rough but this is how I would start it.
Answer by Bolbo13 · Jul 02, 2014 at 02:41 AM
You can implement a Weapon class. Then have all your weapons inherit from it. From there you can create a Weapon array that contains all your weapons. So to sum up the class list should look like this :
Class Weapon
Class Rifle : Weapon
Class Shotgun : Weapon
etc.
Of course every method you call through the Weapon array needs to be defined in the Weapon class (as abstract or virtual) and the specific weapon (rifle, shotgun, whatnot) should override it if need be.
In essence, that's polymorphism at work.
I hope I was clear.
This approach makes it very nicely OO and using Polymorphism could be logic, but from a gamedesign point of view, as they are all weapons, just making small variables pr. type and then keep a "type" variable in them would save you a LOT of work of building new classes everytime you invent a new weapon + you can make weapon upgrades on generic weapons system, you cant on polymorphism architeture, as you would have to make a class for each weapon grade too.
Just imagine the amount of "IF" structures needed if you have 50 different weapons in a game?
You can handle ALL your generic weapons with ONE IF structure, just check the variables and do the math.
So I dont agree with this answer Bolbo13, its a rather bad answer in my opinion.
Inheritance is one valid answer. In many cases changing variables is not enough to create truly unique weapons. A slingshot, knife, bazooka, machinegun, blaster pistol, portal gun, firehose, gravity ray and time warping pen all need different coding structures.
Also its possible to have variables in the class that allow upgrades exactly the same as on a single weapon class.
Your answer
Follow this Question
Related Questions
Changing enum values 1 Answer
C# Array Issue 2 Answers
Weapon switching problem 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers