Hello, so as the title says I have a gameObject and a scriptableObject place for , you guessed it, the scriptableObjects. So what Im trying to do is when a gameObject prefab is instantiated it should reference a random scriptableObject from the ones already created in the folder or whatever.
Any ideas how one can do this?
I’m assuming your referring to instances particular ScriptableObject class, that are listed in the Project windows. If you can get all the instances you want to select from into an array you can select one at random. I can think of two ways to do this.
Assuming your ScriptableObject instances are contained in a Resource folder, you can use Resources.LoadAll method to get all assets in a given directory.
You can create component or another ScriptableObject with a field of type array of your scriptable object type. Once you create an instance of the component or ScriptableObject in the Editor, you can assign your ScriptableObjects to the array field in the Inspector window.
Here’s a simple way of doing it with random generated number:
public SObjects scriptableObjects;
public GameObject anyGObj;
private void Start()
int random = Random.Range(0, scriptableObjects.Length + 1);
GameObject cloneObj = Instantiate(anyGObj, Vector3.zero, Quaternion.identity);
cloneObj.GetComponent<Stats>().name = scriptableObjects[random].objName;
cloneObj.GetComponent<Stats>().health = scriptableObjects[random].health;
After you created an array of scriptable objects, you need to assign them in the inspector.