Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 roberto_sc · Aug 04, 2011 at 07:59 PM · instantiatecrashserializationclone

Crash when using GameObject.Instantiate

I have submitted this to Unity via Crash Report, but I'm posting here because I need an enlightenment/solution for this problem since it's very important and urgent to our project.

A public field or private with SerializeField attribute in a MonoBehaviour will make Unity crash if this field is from an abstract class and someone creates a clone of the MonoBehaviour using GameObject.Instantiate and then call a method from the abstract class, even if the field is not null.

The problem happens because Unity serialize it using the abstract type instead of the actual type.

The following example illustrate this problem, Unity will crash if you hit T:

 public class CloningTests : MonoBehaviour
 {

 [SerializeField]
 private SomethingMaker SomethingMaker;
 
 void Start()
 {
     SomethingMaker = new SecondObject ();
 }

 void OnGUI () 
 {
     Event ev = Event.current;
     
     if (ev.type == EventType.KeyUp && ev.keyCode == KeyCode.T) {
         
         CloningTests script = (CloningTests)FindObjectOfType (typeof(CloningTests));
         
         CloningTests clone = (CloningTests)GameObject.Instantiate (script);
         
         clone.SomethingMaker.DoSomething ();
     }
 }
 }

 [Serializable()]
 public abstract class SomethingMaker
 {
 public abstract void DoSomething ();
 }

 [Serializable()]
 public class SecondObject : SomethingMaker
 {
 public override void DoSomething ()
 {
     Debug.Log ("Doing something");        
 }
 }
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

2 Replies

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

Answer by Bunny83 · Aug 04, 2011 at 09:41 PM

There are several things messy / wrong:

  • Don't use a classname as variable name!

  • Your script clones itself so why are you use FindObjectOfType, "this" will do?

  • Your cloned Object creates a "SecondObject" in Start, but Start is called next frame so you can't access clone.SomethingMaker right after you instantiated it. Use Awake instead of Start. Awake is called when the instantiate is finished.

  • I guess the crash happens because the clone also will produce another clone and so on but I'm not sure.

  • Besides the good will of using class inheritance your setup won't be serialized by Unity. Unity serializes by the variable type, not the actual type. If you have a variable of type SomethingMaker but it holds a SecondObject, after serialization / deserialization it's just a SomethingMaker. That could also cause the crash since your base class is abstract.

I had some bad times figuring out that behaviour. The only working workaround to get those instances serialized is to make them MonoBehaviours and attach them to a GameObject. GameObject-Components are the only classes where Unity serializes the "real" type.

Comment
Add comment · Show 3 · 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 roberto_sc · Aug 05, 2011 at 12:39 PM 0
Share

I'm sorry, I tried to create a simple example from my code but ended up with something not very explanatory. Your first points are right but I won't have any problem because it only clones and access Something$$anonymous$$aker after I hit a key, so that's not what is making Unity crash. Your last point is the cause, and that's what I meant when I wrote "the problem happens because Unity serialize it using the abstract type ins$$anonymous$$d of the actual type". $$anonymous$$aking the abstract class a $$anonymous$$onoBehaviour works very well, thank you very much!

avatar image roberto_sc · Aug 05, 2011 at 02:46 PM 0
Share

The documentation says Unity will serialize all classes inheriting from UnityEngine.Object, so I tried to apply your idea using more basic classes (Object and Component), without success. Do you know if that can be achieved?

avatar image xtin · Oct 11, 2011 at 09:09 AM 1
Share

The crash actually happens because unity tries to access the abstract version of Something$$anonymous$$aker.DoSomething(), which is not a real function. If you make it virtual, it stops crashing.

Unity will also properly serialize derived classes if you derive the abstract class from ScriptableObject, this at least spares the mess of having GameObjects litterd with $$anonymous$$onoBehaviours ;)

avatar image
-1

Answer by leohotluz · Jan 27, 2019 at 09:38 PM

Same problem, I'm giving up using unity, too many bugs.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate scripts with class relationships 1 Answer

Unity object cloning and public/private variables 0 Answers

restricting clone number 2 Answers

How to instantiate from prefab, not from instance? 3 Answers

[SOLVED] How to make to clone an object and make it parent of the transform? 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