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
0
Question by dillyo09 · Oct 21, 2016 at 08:26 AM · typescriptable object

how suppress must be instantiated using the ScriptableObject.CreateInstance method warning?

In my project I am using:

 System.Activator:CreateInstance(Type)

Which throws the following error:

 MyClass must be instantiated using the ScriptableObject.CreateInstance method instead of new MyClass.

This there a way to disable this wrong or a way around it from being called while still using System.Activator.

Comment
Add comment · Show 1
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 edwon · Nov 16, 2017 at 10:17 AM 0
Share

I'm having the same issue

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 16, 2017 at 11:47 AM

You don't want to suppress this error but don't use the Activator. ScriptableObjects (just like MonoBehaviours) must not be created with the constructor. They have a native code counterpart like all UnityEngine.Object derived types. When you create it with new this counter part is missing. That means serialization doesn't work properly and the instance will become a fake null instance.


As the error said:

MyClass must be instantiated using the ScriptableObject.CreateInstance method


If you want to use reflection to create your instances you have two options:


  • Do not derive your class from ScriptableObject and use "normal" classes.

  • If you want to use ScriptableObjects you can simply do a type check before using the Activator. If the type is derived from ScriptableObject ( typeof(ScriptableObject).IsAssignableFrom(yourType) ) you simply use ScriptableObject.CreateInstance instead of the Activator.

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 Bunny83 · Nov 16, 2017 at 11:50 AM 0
Share

Note: $$anonymous$$eep in $$anonymous$$d that ScriptableObjects (as well as any other UnityEngine.Object) type need to be explicitly destroyed using "Destroy" at runtime or "DestroyImmediate" at edit time. All UnityEngine.Object derived types are "tracked" types in the engine core. They can be found again by using FindObjectOfType / FindObjectsOfType. So those objects are not automatically garbage collected.


Again, if this is unwanted behaviour, don't derive your type from ScriptableObject.

avatar image dillyo09 · Nov 16, 2017 at 05:17 PM 0
Share

Thanks for the input I should have answer my own question long ago. This ended up being my solution:

 var weapons = Assembly.GetExecutingAssembly().GetTypes()
                       .Where(t => t.Namespace == "Item.Weapons")
                       .ToList();
 
  weapons.Remove(typeof(Weapons));
 
  for (var i = 0; i < weapons.Count; i++)
  {
        var g = ScriptableObject.CreateInstance(weapons[i]);
        ((Weapons)g).Init();
  }

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

Servers connection differences/uses 1 Answer

Type in bug in editor 1 Answer

Determine if game object is tyoepf One Prefab 3 Answers

Unity4, resolution-independent typography 1 Answer

Array of Transform[] not correctly typed in Javascript 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