You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create new class that inherits from ScriptableObject instead of MonoBehaviour
This will be the data structure of the variables you want to store
usingUnityEngine;[CreateAssetMenu(fileName="New Mana Card",menuName="Cards/ManaCard")]publicclassCard:ScriptableObject{publicnewstringname;publicstringdescription;publicintamountOfMana;publicManaEnummanaType;}
Note: The name field exists already, so we use the new keyword to override it.
Create a new data container based on the Scriptable object from the Asset menu
In this case, choose Cards > ManaCard
Set values for the created asset
Create a few more data containers with different data!
Using the scriptable object
Create a new script inside the GameObject for using the data from the Scriptable object.
In the script, create a new variable with the type of the Scriptable Object:
[SerializeField]Card card
Drag the card template of your choice to the serialized field in Inspector
The data from Card is now available! Access its values with card.amountOfMana, card.name, etc.
usingUnityEngine;publicclassSpawner:MonoBehaviour{// The GameObject to instantiate.publicGameObjectentityToSpawn;// An instance of the ScriptableObject defined above.publicSpawnManagerScriptableObjectspawnManagerValues;// This will be appended to the name of the created entities and increment when each is created.intinstanceNumber=1;voidStart(){SpawnEntities();}voidSpawnEntities(){intcurrentSpawnPointIndex=0;for(inti=0;i<spawnManagerValues.numberOfPrefabsToCreate;i++){// Creates an instance of the prefab at the current spawn point.GameObjectcurrentEntity=Instantiate(entityToSpawn,spawnManagerValues.spawnPoints[currentSpawnPointIndex],Quaternion.identity);// Sets the name of the instantiated entity to be the string defined in the ScriptableObject + a unique number. currentEntity.name=spawnManagerValues.prefabName+instanceNumber;// Moves to the next spawn point index. If it goes out of range, it wraps back to the start.currentSpawnPointIndex=(currentSpawnPointIndex+1)%spawnManagerValues.spawnPoints.Length;instanceNumber++;}}}