- Home /
 
               Question by 
               AlfAtUnity · Jul 03, 2013 at 09:18 AM · 
                javascriptscriptableobject  
              
 
              UnityScript ScriptableObject error
In C# it's a piece of cake to create instance of ScriptableObject. I try to do the same with UnityScript.
 //MyMonoBehaviorClass.js
 var mScriptableObject = ScriptableObject.CreateInstance(typeof(MyScriptableObjectClass));
 mScriptableObject.foo();
 //=================================================================
 //MyScriptableObjectClass.js
 class MyScriptableObjectClass extends ScriptableObject
 {
     function foo()
     {
         print("Hello from foo()!");
     }
 }
I get error: "ScriptableObject.CreateInstanceFromType can only be called from the main thread." What's wrong?
               Comment
              
 
               
              Answer by AlfAtUnity · Jul 03, 2013 at 10:50 AM
 #pragma strict
 var  mScriptableObjectClass :  ScriptableObjectClass;
 function Awake()
 {
     mScriptableObjectClass = ScriptableObject.CreateInstance(typeof(ScriptableObjectClass));
 }
 function Start()
 {
     mScriptableObjectClass.foo();
 }
If you're writing c#, don't mix in js. A c# class looks like this:
 using UnityEngine;
 using System.Collection;
 public class SomeScriptableObject : ScriptableObject
 {
     void Start()
     {
         // some code
     }
 }
Oh, actually that was JS code that wasn't formatted properly. I already made a correction.
Your answer
 
 
             Follow this Question
Related Questions
Proper way to save ScriptableObjects changed during play mode 2 Answers
Image sprite loaded from scriptable object ignores color 0 Answers
How to use "SerializeObject" with an object which doesn't derive from Object? 1 Answer
Variable will not change? 1 Answer
Unique function for each instance of a scriptable object 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                