I have a few different scriptable object types - examples: motherboardSciptableObject, cpuScriptableObject, gpuScriptableObject, psuScriptableObject, etc. I have a script that spawns out buttons from the scriptable objects and is working. I have a listener on each button that is going to call a method in another script and pass in the ScriptableObject in the parameters (code below). This method that it calls is generic by design as it will increment components, check cost of components, etc so ScriptableObject type shouldn’t matter. I’m having issues sending the ScriptableObject in and then retrieving data from these objects. Examples below -
public void PurchaseComponent(ScriptableObject objectToPurchase)
{
// This is the receiving method in the other class. Due to multiple types of scriptable objects I have to pass in the ScriptableObject type.
Debug.Log(objectToPurchase.objectCount);
}
In the above code, different types of ScriptableObjects are getting passed in (motherboardSciptableObject, cpuScriptableObject, etc) but the parameter takes a ScriptableObject. How do I go about “converting” these objects back to their original scriptableobject type so I can retrieve the data within? Currently all objects in that method are of type ScriptableObject. Running objectToPurchase.GetType() returns “MoterboardScriptableObject” but I can’t do much with it. Trying to output info from the ScriptableObject results in the above code saying “Cannot resolve symbol objectCount”.
I’m fairly new with C# and Unity, but have been scratching my head the past couple days on how to solve this issue. I’ve Google’d but can’t seem to come up with the correct search terms to figure out what I’m doing wrong.