- Home /
how to make/use ScriptableObject?
I've been trying to solve a mans Question Learned I cannot help him any longer but out of curiosity I went looking in link @meat5000 provided and I found something I've never saw ScriptableObject before
A class you can derive from if you want to create objects that don't need to be attached to game objects. - am what? how do I make 1 if it is not attached to non GameObject, IF I manage 1 how do I access it?
This Answer Says he can make 2.35M Scriptable Objects before Unity Crashes
also I've been looking through this
and this it has a code in it and I don't understand more than 40% of it
also I've been asking my self:
How to create 1?
how to keep track of ScriptableObjects?
how to access them?
where could I see each scriptable Object in hierarchy?
IF it's made and it has MeshRender attached where will it render it? if it has not got Transform attached?
this Question is made purely out of curiosity.
Answer by fafase · Oct 27, 2013 at 04:55 PM
I guess the idea is to have an object that is not a component. So it is in memory like any object but not on any GameObject. Meaning you won't see it in the Inspector nor in the Scene list.
You create one like this:
public class NewBehaviourScript : ScriptableObject {
int a = 10;
public int A{
get{return a;}
}
}
and you use like this:
public class Trial : MonoBehaviour {
NewBehaviourScript newGuy;
void Start ()
{
newGuy = (NewBehaviourScript)ScriptableObject.CreateInstance(typeof(NewBehaviourScript));
print(newGuy.A);
}
}
You keep track of it via the reference you assign it to. Just like basic programming. It is just a storage class for values and methods, I don't think it is meant to have a mesh. It does not seem to have any update so you would have to update via your own call.
If you look at the inheritance hierarchy it has nothing to do with the whole MonoBehaviour apart from being an Object.
So, it is just for storing info and helpers.
My point of view.
EDIT: it then makes sense to be able to create millions of them since they are just taking space in memory but no resources, no update call, no Unity maintainance, only GC when needed.
thanks you've been very informative :)
it can be done like this too right?
public NewBehaviourScript newGuy;
so it actually isn't an object
but different kind of class
since you have to call it from $$anonymous$$onoBehaviour
but somehow calling it is much more complex than class or even structure
I usually was using a class for this
I wander what would they be using millions of ScriptableObjects for, ...
It is an object and it is not called from $$anonymous$$onoBehaviour. It actually has nothing to do with $$anonymous$$B. It is parallel to it on the engine inheritance.
public NewBehaviourScript newGuy;
This is just a reference, but no data.
newGuy = (NewBehaviourScript)ScriptableObject.CreateInstance(typeof(NewBehaviourScript));
will find some memory, create the object and store the address into newGuy.
Somehow it is the same as:
newGuy = new NewBehaviourScript();
For some reason, Unity provide a fake ctor with CreateInstance probably because they have internal maintenance on the object.
The reason why you should use ScriptableObject over basic object is long explained here: http://answers.unity3d.com/questions/190350/what-is-the-purpose-of-scriptableobject-versus-nor.html