• 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
Question by Stardog · Dec 03, 2012 at 07:37 PM · instantiateinheritancemonobehaviourfsm

Instantiate without inheriting from MonoBehaviour

I'm using a state machine. There is a "ClassName : FSMSystem" that is attached to a GameObject. Then I have "StateName : FSMState", but I can't use anything from MonoBehaviour in this state, only in the main FSMSystem class.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : FSMSystem {
 
     // STATES
     public PlayerIdle S_PlayerIdle;
 
     void Awake()
     {
         AddState(S_PlayerIdle);
         S_PlayerIdle.Parent = this;
 
         GoToState(S_PlayerIdle);
     }
 }

FSMSystem inherits from MonoBehaviour, so it has access to Instantiate. FSMState does not. Here is an example state script:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class PlayerIdle : FSMState {
 
     [System.NonSerialized]
     public PlayerScript Parent;
 
     public override void OnEnter()
     {
         
     }
 
     public override void OnUpdate()
     {
         
     }
 
     public override void OnExit()
     {
         
     }
 
 }

I can't make FSMState inherit from MonoBehaviour, so how do I get access to Instantiate/etc?

Comment
$$anonymous$$
tezza2k1
gtvx
shpand
DragonCoder
samueljr18
unity_bTkJGG31azdikQ

People who like this

7 Show 2
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 fafase · Dec 03, 2012 at 08:08 PM 0
Share

Why can't you make FSMState inherits? If there is already another class it inherits from, can you make that one an interface? C# does not support multiple inheritance of class but does with Interfaces, as many as you want.

avatar image Stardog · Dec 03, 2012 at 08:46 PM 0
Share

Actually, making FSMState inherit from FSMSystem, which itself inherits from MonoBehaviour seems like it might work. I will go check...

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Stardog · Dec 04, 2012 at 12:48 AM

The solution was to use GameObject.Instantiate instead.

 GameObject instancedObj = GameObject.Instantiate(..., ..., ...) as GameObject;
Comment
Napoleonite
Wompipomp
NuKeR2k
brentstrandy
$$anonymous$$
Erdogan
tezza2k1
eboucher4
ajayajayaj
AfroDieter
EyePD
n_i_e
gtvx
StuwuStudio
neoRiley
And 14 more...

People who like this

29 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 Owen-Reynolds · Dec 04, 2012 at 02:12 PM 2
Share

Or just `Object.Instantiate`. GameObject works since it's a type of Object. Since your Q specifically mentioned Monobehavior, you could even use that.

The trick is that Instantiate is a static, so you don't have to be an instance to use it.

avatar image m0nkeybl1tz · Jun 17, 2017 at 10:30 PM 0
Share

I know I'm about 5 years late to this party, but thank you! I was able to inherit from MonoBehaviour, but I wasn't able to create an instance of my class using "new". This totally solved it for me.

avatar image

Answer by nventimiglia · Dec 03, 2012 at 08:38 PM

You can create new instances of non monobehaviours by using new.

public class NonMonoClass{}

void Awake(){ var myObject = new NonMonoClass(); }

Comment
StuwuStudio

People who like this

-1 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Can I call a class's method which inherit monobehaviour by a normal class? 0 Answers

Abstract Class - Monobehaviour Functions 0 Answers

How to serialize fields for both base and derived classes? 0 Answers

Start being called after Update of another GameObject? 0 Answers

Using AddComponent to add a Sub Class using a String 4 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