Hi there and thanks for looking the question I have is this. I have a game where you can unlock 11 weapons I was wandering if there is an easier way to do it compared to the way I have also with a mouse scroll weapon selection. The way I have it I am sure is not optimised and i'm sure it wont work the more weapons I add all i want is some advice on the best way to do this whether that be separate weapon scripts or what. Here is the code not finished but want advice before i carry on
I recommend learning about classes and arrays.
Your project is screaming for you to define a class to represent a gun.
Here is an example of what that might look like:
If you define a class named Gun like that, you can then put the following at the top of your script:
This will declare an array of guns that you can maniplulate in your script. You can edit the guns in the array in the Inspector as well.
Learning about arrays is a must. They are one of the most important, useful, and fundamental concepts of programming.
Once you've defined your gun class and put some data in it, you can process the array in your script like this:
In C# it would be like so
and when you wanted to set them all to false you could just write
but you could take this one step further if you created a Gun Class, and then create a class that holds all the Guns, so you would have all your guns (RayGuns, Lasers) in one list, allowing easy setting to true or false.
answered Oct 24 '11 at 09:02 PM