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
1
Question by parahunter · Feb 03, 2011 at 09:37 PM · objectclassinheritanceassignfinite-state-machine

How to make classes in C# that are assignable to public variables in scripts trough the Editor

Hello I am trying to make a modular finite state machine/flocking system where each state is a class that should be able to be assigned to an array of states in the MonoBehaviour that is responsible for executing the states. I have read in the Scripting Reference that if a class inherits from Unity's Object it should be able to be assignable in the editor so I went ahead and did this. Basically I have two classes, one that states inherits from called BaseState and one that can contain states(right now this is called Sheep):

public class BaseState : Object {

}

public class Sheep : MonoBehaviour {

 public BaseState[] states; //states should be allowed to be dragged to this place

}

To assign the Sheep class as a component to a GameObject works fine but I am not allowed to assign the BaseState class to any elements in the states array. What is weird is that Unity can see that BaseState is inherited from Object so the array is shown in the Inspector. Another thing is that it works if I change the type of the states array to Object. So my question is, How should a class be declared in order for Unity to let it be assigned to variables in the Inspector?

Any help would be much appreciated

Comment
Add comment
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

1 Reply

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

Answer by yoyo · Feb 03, 2011 at 09:48 PM

Note that you don't assign the BaseState class to elements of the array, you would assign instances of the BaseState class. Normally you get instances of objects from the assets in the Project view or from objects or components in the Hierarchy view. In your case, you don't have any instances of BaseState or a way to create them.

Couple options ...

You could derive from MonoBehaviour, not from Object. Then you can create an object and assign the component to it, then drag the object onto an element in your states array. In this case, the states array is an array of references to (MonoBehaviour component) objects.

Or you could make BaseState a [System.Serializable] class (it doesn't need a base class), give it whatever fields you like, and create an array of BaseStates, as you've done. In this case your Sheep object will own the array and it's properties -- the array will contain the data itself, not references to objects containing the data.

Hope that helps.

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 parahunter · Feb 03, 2011 at 10:38 PM 0
Share

Thank you, I feel quite stupid for not having spotted that myself :-) I will go with your first option since that would also allow me to have a set of steering behaviour prefabs for each type of entity. These could then use the same steering behaviour scripts as all the other sets of prefabs use.

avatar image yoyo · Feb 03, 2011 at 11:17 PM 0
Share

Don't feel stupid, it takes a while to wrap your head around everything. This thread might be be helpful ... http://answers.unity3d.com/questions/36132/whats-the-difference-between-the-component-and-gameobject-classes-and-how-they-ar

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

No one has followed this question yet.

Related Questions

Overriding an inherited Enum? 1 Answer

Type 'Object' does not support slicing 1 Answer

add class or script to child gameObjects 1 Answer

How to save prefabs with private data through inheritance 1 Answer

how can I make a box with flat cubes(which I want to call a 'tile')? and have each tile detect collision? 0 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