![]() How can I use OnEnable on ScriptableObjects to populate my factory with the types it will dispense? Instance = /*SomeRandomGameObject*/.AddComponent() public static MonoBehaviourFactory Instance But the issue with that approach is that the instance of the MonoBehaviourFactory would not have the references dropped in from the inspector such as someInspectorText. I don't know of a good way of instantiating the factory pre-awake, I thought about using something like the following. Script execution order does not improve the situation. In it's awake have something like: var thingTypes = Assembly.GetAssembly(typeof(ThingType)).GetTypes().Where(thingType => thingType.IsClass & !thingType.IsAbstract & thingType.IsSubclassOf(typeof(ThingType))) īut now I'm stuck - I have the classes for ThingTypes, like, ThingAType but not the individual SOs.Īnd an additional problem, is that OnEnable is called before my factory's Awake, so it is not initialized yet. The second approach is to have the factory be a MonoBehaviour, and do reflection magic. I don't understand this exception, it makes sense to me that if OnEnable is called before the scene loads, and then the SO is destroyed at scene load. (Did you spawn new GameObjects from OnDestroy?) Some objects were not cleaned up when closing the scene. The third problem is that I'm receiving this exception: ![]() The second problem is that OnEnable is called only when the scene has a MonoBehaviour referencing that ScriptableObject asset which isn't always the case.Įven now when I'm reproducing the behaviour in a small project I can't put my finger on the exact requirements for OnEnable to be called. The first is that OnEnable is called from the editor, I've gotten around that with the if UNITY_EDITOR. Public abstract class ThingType: ScriptableObjectĭebug.Log("Enabling ThingType of type: " + this.name) NewThing.GetComponent().sprite = thingType.sprite NewThing.GetComponentInChildren().text = thingType.name GameObject newThing = MonoBehaviour.Instantiate(thingType.prefab, ) Public void RegisterThing(ThingType thingType) Private static ThingFactory instance = null I've explored two approaches, one is having the factory not be a MonoBehaviour: So ideally something like each SO registering itself in the factory on it's awake would be great. I'd like to not need to append to the Factory each new SO that I create. In this case, a custom game event can be created which would carry all the necessary data.My factory creates instances based off ScriptableObject's ("SO") properties. For example if a custom type needs to be passed as an argument to the event. In some situations, built-in game events might not suffice.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |