Hey Guys,
I obviously don’t know how to solve this
I have 3 Classes
A, B and C
C : B
B : ScriptableObject
A : MonoBehaviour
in A theres a List
List<B> bList = new List<B>();
now I want to put C into this List with it’s own values
public class A : MonoBehaviour
{
List<B> bList = new List<B>();
public void AddToList(B bElement) //where I want to add C (or even D : B)
// maybe I could call this function this way: A.AddToList(C);
{
//and there is the problem - I have to create an Instance of C
//with
//bList.Add(null);
//bList[0] = (C) ScriptableObject.CreateInstance(typeof(C));
//but if I do this, no values of C are passed and also it could be D instead of C
}
}
Does anyone know how to solve this?
Thanks in advice.
If C derives from B
C : B
Then you should be able to automatically put it into any container that holds B.
C myC:
bList.Add (myC);
This is useful when you have methods in B that are overridden in C.
Class B
SetupObject ()
{
//assign variables of B
}
Class C : B
override SetupObject ()
{
//assign variables of B
//assign variables of C
}
@RobAnthem had some great advice:
For objects that DON’T derive from the same base class
if B and C derive from the same thing then the list will accept either. If they do not, then only a List will work. Then you can parse out the data like this.
if (myList*.GetType() == typeof(objectB)*
{
(myList as objectB).BProperty1 = 42;
}
## ScriptableObject ##
> Unity can’t serialize non-Unity based
> objects. If you want to serialize this
> use binary or convert it into a
> ScriptableObject. The main thing about
> ScriptableObject though is that it
> requires each instance is created AND
> stored. Therefore you would need to do
> something like this.
AssetDatabase.CreateAsset(newRoomObject, “Assets/Resources/Rooms/” + newRoomObject.RoomName + “.asset”);
RoomObjects tempRoom;
AssetDatabase.AddObjectToAsset(tempRoom = CreateInstance(), “Assets/Resources/Rooms/” + newRoomObject.RoomName + “.asset”);
newRoomObject.requiredObjects.Add(tempRoom);
100% Credit to @RobAnthem for the above. Thank you!