I haven’t finished trying all possibilities, but I can’t seem to get threads to start on a ScriptableObject. I StartCoroutine on them all the time using a reference to a MonoBehavior that I know won’t be disabled, perhaps threading runs into similar limitations? You can also for example call a public Update method on a ScriptableObject from a MonoBehaviour to mimic the normal Update behavior, but it doesn’t inherently MoveNext anything on it’s own… Still… does a thread require something of that nature? simple myThread.Start() calls don’t seem to be doing anything.
new System.Threading.Thread(() => LoadSection(x, z)).Start();
}
void LoadSection (int x, int z) {
Debug.Log("Thread started");
}
There are no issues using threading in or on ScriptableObjects besides the common Unity limitations and common thread safety. In other words ScriptableObjects have to be created with CreateInstance, not with “new”. They can only be created on the main thread. Also all properties that come from Unity (like the “name” property for example) can only be accessed from the main thread. Besides that from a pure Mono / .NET point of view a ScriptableObject instance is just a class like any other class.
Though you have to be careful with threads especially when testing in the editor. You have to make sure you terminate all threads properly. Stopping playmode will not stop any threads you started that haven’t finished yet.
I’ve made a quick test case with two classes:
// monobehaviour attached to a gameobject in the scene
using UnityEngine;
public class ScriptableObjectThreadingComponent : MonoBehaviour
{
void Start ()
{
var obj = ScriptableObject.CreateInstance<ScriptableObjectThreading>();
obj.StartThread();
}
}
This is the scriptableobject
using UnityEngine;
public class ScriptableObjectThreading : ScriptableObject
{
public void StartThread()
{
new System.Threading.Thread(()=>ThreadMethod()).Start();
}
void ThreadMethod()
{
Debug.Log("Thread started");
}
}
I properly get the “Thread started” log in the console. You may have omitted important details in your test case.