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 magnusm · Nov 02, 2013 at 12:33 PM · javascriptarrayvariableclassassign

Creating A Class Variable That Works With Any Class

Hey there :)

I am not sure how to ask this, so I have some rough code to help explain. Pretty much I have many different classes for each type of 'Mod', so each button I have to write out what class to look for, which take require lots of code per button.

These are all the classes for each type of Mod:

 var attackMods : AttackModClass[];
 var cockpitMods : CockpitModClass[];
 var defenceMods : DefenceModClass[];
 var intelligenceMods : IntelligenceModsClass[];
 var mobilityMods : MobilityModsClass[];
 var structuralMods : StructuralModsClass[];
 var utilityMods : UtilityModsClass[];


Is there any way I can I have a generic class variable, like this:

 var selectionMod : Class[] //< what do I name this?



Then just set a class to that variable whenever a button is click, eg:

 //button 1 clicked
     selectionMod = attackMods;
     selectionMod[0];
 //button 2 clicked
     selectionMod = defenceMods;
     selectionMod[0];
 //button 3 clicked... etc


And NOT do this, which is what I am currently doing for each button:

 //button 1 clicked
     x =0
     attackMods[x].name
     attackMods[x].id
     attackMods[x].value
    .. etc
    
 //button 2 clicked
     x =0
     defenceMods[x].name
     defenceMods[x].value
     defenceMods[x].id
    ..etc

 //button 3 clicked.. etc

This would result in a lot less code in every button, making everything easier to edit.

It's quite hard for me to explain, so if you do not understand I can explain further :)

Thanks, Magnus

Comment
Add comment · Show 4
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 Fornoreason1000 · Nov 02, 2013 at 12:42 PM 0
Share

a class that can be accesed by anyclass is a static variable...

 class $$anonymous$$yClass {
 static int $$anonymous$$yVar
 
 }
 
 $$anonymous$$yClass.$$anonymous$$yVar //can be used anywhere

now the problem m with static variables is that they NEVER get collected until the program is ter$$anonymous$$ated even if no instances of $$anonymous$$yClass exist $$anonymous$$yVar will be in $$anonymous$$emory e.g Exit to Windows.

what i did was make an abstract base class , then make a generic list of that abstract class and forced all mod-classes to inherit from the abstract class. then the compiler will always know that the values you are using for that list will be there. RuntimeGUI uses this to handle interfaces.

alternatively you can use dynamic typing or object.

avatar image DannyB · Nov 03, 2013 at 11:50 AM 1
Share

It looks like what you need is inheritance. So you will have a $$anonymous$$odsClass that extends $$anonymous$$onoBehaviour as usual, and all your other classes, will extend it ins$$anonymous$$d.

avatar image Fornoreason1000 · Nov 03, 2013 at 09:17 PM 0
Share

DanyB not just inheritance, if did it this way it would just be a list of $$anonymous$$odclass, anything that is e.g attack$$anonymous$$ods is cast down thus removing its special properties. there is a wiki though...

http://en.wikipedia.org/wiki/Template_method_pattern

avatar image magnusm · Nov 10, 2013 at 08:33 PM 0
Share

Sorry guys, I really appreciate the replies :) Haven't got around to experimenting properly with your feedback.

I will do when I get a chance, Thanks :)

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Different way to access class variables? 1 Answer

Possible to use an array value as the name of a variable? 2 Answers

Pushing GameObject into JS Array returns NullReferenceException 1 Answer

Why Does Initialising Built-In Array of Classes Require Constructor? 1 Answer

How do I create an array for multiple targets? 1 Answer


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