
Let’s set the default file name when creating a new scriptable object to be WeaponScriptableObject and the menu name to be something like ScriptableObjects.

If you head back into the Editor like this, Right-click on the Project window > Create, it should show up as your first option.Īlthough this works fine, for the sake of organization let’s add a few more properties. WeaponScriptableObject.cs using System.Collections Īrticle continues after the advertisement: And to do that we need to add an attribute to the class called. Now, all that’s needed is to define a way to create a scriptable object from this template in the assets folder. Public class WeaponScriptableObject : ScriptableObject WeaponScriptableObject.cs using System.Collections Let’s copy over all of the variables that won’t change during runtime, this is because ScriptableObjects save the value during runtime and thus the variable will not be reset even when the game is restarted. Now if you take a look at your WeaponController script, you should see the stats we created for the weapon previously. Make sure to also delete away the Start() and Update() functions as we have no need for them. So if you recall back to the previous part about inheritance, you would know that all we have to do is just derive from ScriptableObject instead. However, we want a ScriptableObject that doesn’t sit on the game object. I could on and on about ScriptableObjects and their various benefits, but for the sake of simplicity let’s dive right in.įirst up, create a new C# script called WeaponScriptableObject inside of the Weapon subfolder in Scripts and open it.īy default all scripts in Unity derive from Monobehaviour as it allows the script to be a component of an object. This is different from a prefab as a prefab only saves values after stopping play mode and not during play mode. What that means is that if you were to play the game and change a value of a ScriptableObject, the value will be saved. And since they are independent, any game object within any scene can access the ScriptableObject which makes it convenient for referencing data, there won’t be any need to reference a certain script or game object.

ScriptableObjects are also independent from anything else within the game. This makes them more convenient because we can easily duplicate them and test for different stats. This means that they are stored within your assets folder as a Project-level asset.

Monobehaviour and a ScriptableObject is that ScriptableObjects are an asset. The biggest difference between storing stats in a script i.e. They act as templates for objects and are most notably used to store stats like the ones mentioned previously. ScriptableObjects are pretty much data containers used to contain data we need for the game to function. However for the sake of the tutorial I’ll still give you the basic rundown as to what exactly they are.
#Unity ai with scriptable objects how to#
If this is your first time hearing or working with ScriptableObjects, I recommend you check out Brackeys’ or samyam’s video on them, they are extremely informative and teach you the ins and outs about how to use ScriptableObjects. Luckily for us Unity already has something built in that fits all our needs, and that is ScriptableObjects. Weapon scriptable objectsīefore we begin creating any form of stats, we first have to understand what we are trying to achieve.

Let’s start with creating stats for our weapons we made in the previous part. So in today’s part, we are going to be taking a look at how we can create scalable weapon and enemy stats and also touch on how we can make use of weapon stats to damage enemies. You might think this is a minor issue, but as you get more into creating new weapons and enemies, you will soon realize that it is better to have a more convenient way to test different values. However, these aren’t really expandable and they are kind of a nuisance since we always have to reference back to the script whenever we want a stat of a weapon and enemy per say.Īdditionally, testing will be a lot harder as we need to create 2 of the same prefabs to compare a of batch values. If you recall back to the previous part, we have already created some stats for our weapon and enemies while working on their respective scripts. They define many different things such as speed and attack or defense and health. Statistics makes up a huge part of Vampire Survivors just like any other game.
