Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
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
Add comment · 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 FS$$anonymous$$State 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 FS$$anonymous$$State inherit from FS$$anonymous$$System, which itself inherits from $$anonymous$$onoBehaviour seems like it might work. I will go check...

2 Replies

· Add your reply
  • Sort: 
avatar image
27
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
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 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 $$anonymous$$onobehavior, 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 $$anonymous$$onoBehaviour, but I wasn't able to create an instance of my class using "new". This totally solved it for me.

avatar image
-1

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
Add comment · 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.

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

Abstract Class - Monobehaviour Functions 0 Answers

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

Strategy Pattern with Monobehaviours 2 Answers

Finite States Machine (Inheriting question) 0 Answers

Previously functional script now mysteriously does not invoke Start or Update methods 2 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