Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Major · Jul 02, 2014 at 12:48 AM · c#arrayweaponswitchenable

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BerggreenDK · Jul 02, 2014 at 02:46 AM 0
Share

this question answer system is for "problems" not "design of my code". Ask in the normal forum for this kinda question.

avatar image BerggreenDK · Jul 02, 2014 at 02:47 AM 0
Share

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.

avatar image Kiwasi · Jul 02, 2014 at 03:38 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Major · Jul 03, 2014 at 03:45 AM 0
Share

After a little tweaking, this works perfectly. Thanks!

avatar image Lazdude17 · Jul 03, 2014 at 03:45 AM 0
Share

No problem! Glad it worked.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BerggreenDK · Jul 02, 2014 at 02:43 AM 0
Share

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.

avatar image Kiwasi · Jul 02, 2014 at 03:28 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges