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
3
Question by Steven-Walker · Dec 16, 2010 at 06:47 PM · scriptableobjecteditorwindow

Create editor window with ScriptableObject.CreateInstance

I'm positing this as an answer, not a question, because I couldn't find the answer anywhere else.

Problem: If you have a custom editor window (class extends EditorWindow) and try to invoke it like this:

var window : MyCustomWindow = new MyCustomWindow();

You'll get the following warning upon invocation:

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

However, there is no documentation to appropriately instruct how to do this. My first attempt was this:

var window : MyCustomWindow = ScriptableObject.CreateInstance("MyCustomWindow");

Which produces the error:

Instance of MyCustomWindow couldn't be created because there is no script with that name.
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 Ilya · Mar 29, 2011 at 05:22 PM 0
Share

Thank you, this helped me a lot! :)

4 Replies

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

Answer by Steven-Walker · Dec 16, 2010 at 06:48 PM

SOLUTION:

var window : MyCustomWindow = ScriptableObject.CreateInstance(typeof(MyCustomWindow));

Hopefully this saves someone else a little time.

Comment
Add comment · Show 1 · 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 snake5510 · Jun 01, 2020 at 11:35 AM 0
Share
 $$anonymous$$yCustomWindow window = ($$anonymous$$yCustomWindow )ScriptableObject.CreateInstance(typeof($$anonymous$$yCustomWindow ));

10 Years old answer but still works Thanks.

avatar image
2

Answer by GuyLSmith · Feb 06, 2012 at 03:51 AM

Here is a solution that works for dynamically creating the object given a string. This is for C#.

 MyBase v_new_instance = null;
 Type v_type = Type.GetType(v_class_name);
 if (null != v_type) {
     v_new_instance = (MyBase)CreateInstance(v_type);
 }
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
avatar image
0

Answer by GuyLSmith · Feb 06, 2012 at 03:51 AM

Yes, well, what if all I have is a string containing the type? I need to dynamically create an object based on a string selected from a list of script names read from a directory. This means I can't statically specify the type as shown in this solution. I need to be able to call CreateInstance with a string - from C#.

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
avatar image
0

Answer by caesarpena · Sep 27, 2016 at 07:49 PM

now this is the way

GUISkin a = ScriptableObject.CreateInstance();

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I display the fields of a ScriptableObject in an editor window? - Any SO even if I don't know the fields within. 1 Answer

Save ScriptableObject On Unity Application Quit? 1 Answer

Undo issues in EditorWindow 0 Answers

Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically. 2 Answers

ScriptableObject does not serialize 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