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 Kenned · Sep 10, 2013 at 09:08 PM · arrayinspectorclasscustomabstract

Array of abstract class in inspector

Hey people :)

I'm kinda new to the whole making custom inspector scripts, but I have this problem that I was wondering if is even solvable.

So what I want to do is have multiple classes extending from my Skill class which are all unique, this is not a problem, however I'm looking to be able to attach these to a character through the inspector into an array of Skill.

Is that possible and is there a smarter way of doing it? :)

I have a class which contains the line of code that looks like this:

public Skill[] mySkills = new Skill[8];

And the abstract class Skill which looks like this: public abstract class Skill{

     public bool targetMyTeam;
     public bool targetMustBeEmpty;
     protected string name;
     
     public enum targetPattern{
         singleTarget,
         teamTarget,
         allTarget,
         randomTarget,
         lineTarget,
         rowTarget,
         singleTargetFront,
         lineTargetFront,
         adjacentTarget,
         crossTarget,
         noTarget
     }
     
     protected targetPattern myPattern;
     
     public abstract bool Action(BattleParticipant[] target);
     public abstract void UpdateStat(int neoStat);
     public abstract string GetName();
     public abstract targetPattern GetTargetPattern();
     
 }
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 Hoeloe · Sep 10, 2013 at 11:02 PM 0
Share

You might have to add the [Serializable] tag to each of the classes you want to use, and possibly the abstract one, too. It is possible, however, that this behaviour is not possible, though I sincerely hope it is, as it has many potential uses.

avatar image Kenned · Sep 11, 2013 at 06:18 AM 0
Share

Indeed, I believe I have tried doing this, but I'll just try it out one more time :3

Hmm I tried adding the [System.Serializable] to both my abtract class, my inhertitances of that class, my Player and BattleParticipant which is were I want to make it visible, but it didn't work :/

avatar image Fattie · Sep 11, 2013 at 07:18 AM 0
Share

Just a random thought, perhaps useless, is it possible Func would work better than Action here??

avatar image Kenned · Sep 11, 2013 at 09:01 AM 0
Share

Thanks for the comment Fattie :3 However I believe Actions is better suited for this specific case ^^

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by whydoidoit · Sep 11, 2013 at 07:31 AM

I'd be tempted to define those attributes in an interface and have your components implement that interface - Unity has problems with serializing polymorphic objects see this: http://answers.unity3d.com/questions/14877/can-a-custom-inspector-serialize-a-list-of-derived.html).

If you put them in an interface you can just attach any "skills" you like to any game object and find them all easily using something like this:

   using System.Linq;

    ...

    foreach(var skill in GetComponentsInChildren<Transform>(true).Where(t=>t is ISomeInterface)).Cast<ISomeInterface>())
    //etcetera
Comment
Add comment · Show 1 · 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 Benproductions1 · Sep 11, 2013 at 09:12 AM 0
Share

I was going to suggest interfaces... don't know why I left that out XD +1

avatar image
1

Answer by Benproductions1 · Sep 10, 2013 at 11:38 PM

Hello,

For any class to be attachable to a GameObject, it needs to inherit from MonoBehaviour or even just from Component. There is no way around this.

Also note that if you want to add a script to a GameObject that does not match the name of the file that contains it, you will need to do it manually using AddComponent. I haven't tested anything, but you should be able to make a list (don't make an array) of an abstract class.

Hope this helps,
Benproductions1

Comment
Add comment · Show 1 · 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 Kenned · Sep 11, 2013 at 05:53 AM 0
Share

Thank you for your quick response :) I think I got what you mean, but for whatever reason, I took a step closer to achieving what I needed. I now have another problem :/

Here's an image of my inspector, where I finally got it to show my Skill array by inheriting from $$anonymous$$onobehaviour :) However if I try to add a Class which extends from Skill, such as _SkillFire into the Array, it won't let me :)

http://i.imgur.com/bS59UDx.png

Do you have an idea of why that might be? :3

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

20 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

Related Questions

Custom Class Array Serialization Problem 0 Answers

How to serialize an array of classes 1 Answer

Public Array with a class not showing in Inspcetor 1 Answer

"Null Reference Error" when using a custom class as an array 1 Answer

Error message when accessing class array 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