- Home /
The question is answered, right answer was accepted
[SOLVED] Running unique ScriptableObject instances
I've been doing the tutorial from Unity on ScriptableObjects and reached an impase when I finally realized that ScriptableObjects are not like Monobehaviours in the sense that they are unique to the GameObject they operate on.
For example, I've made a Wander action where the Ai would move randomly in the scene. If I tell 2 Ai's to use this action they will all move towards the same random position generated by the script. Same goes with attacking the player. If I have an Ai that spotted the player (which means that its close by) it will move in to attack, but if this Ai spotted the player even the Ai that's on the other end of the scene would have "spotted" the player and change his state from idle or wander into attack. This is of course not the behaviour I wish from the Ai.
I've scoured the forums and answers and the only answer I could find was to clone the ScriptableObjects:
ScriptableObject clone = Object.Instantiate(scriptableObjectAsset);
What I did with the solution above is that instead of using:
CurrentState.UpdateState(this);
I create a newState that instantiates the CurrentState and updates that newState (aka, the clone).
newState = Object.Instantiate(CurrentState);
newState.UpdateState(this);
This however still does not solve the issue. Two different Ai's with a clone of the "wander action" will move to the same position, two different Ai's in different locations of the map / scene will "see" the player and move in to attack and so on.
What am I doing wrong here? Should I just revert back to monobehaviours for the AI?
Edit: In the end it seems like I need to consider a different approach for the AI. The answer that Haxagonius offered below is what clarified it for me.
Scriptable Objects behave quite the same as $$anonymous$$onoBehaviours. Each instance is unique, like each instance of a $$anonymous$$onoBehaviour is. The difference is, Unity takes care of creating multiple instances of $$anonymous$$onoBehaviours, while for ScriptableObjects, that's your task. I would think it this way. $$anonymous$$onoBehaviours for actual different instances, ScriptableObjects for different types of things.
So if I want GameObject A to do a different action from B I'd need 2 different ScriptableObjects that run the same code? As in, even though it has the same code I have 2 ScriptableObject named ActionA and ActionB for each GameObject?
Answer by hexagonius · Jun 13, 2017 at 10:56 AM
If your scriptable objects have state (variables) then yes. It's just as if you wanted one of your characters to do two different things at once. Doesn't work. If you pull the state out of the objects and into your characters, meaning having the logic only, then it should work. Otherwise, yes, you need multiple instances. But if that's the case, scriptable object might not be the right choice
I understand. Then it does seem like ScriptableObjects is not what I need here. Thank you for clarifying this.