AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
![]() ![]() If (DemoScriptableHashCodes. check guid to avoid complex comparison If (!DemoScriptableHashCodes.Contains(guid)) There are many ways to store such data someone stores it in tables, in XML or JSON files that edit with their own tools. In RPG there is a database of items, in match-3 the cost in the crystals of tools from the store, in action hit points, for which medical kit heals. can be HashCode, but this is not guaranteed to be unique. Each game has data that game-designers work with. Public static List DemoScriptableHashCodes = new List() ![]() Public static List DemoScriptables = new List() This can also be a dictionary, but this way you have list and can run a for or foreach loop. Public class DemoScriptable : ScriptableObject Add this method to any class to see the log print when the game starts.ĭebug.Log("The game has started and this class is Initialized") Īs pointed out by derHugo: The attribute initializes the class itself so it would not work as a way to add instances to a listĪnother possible other solution: If you want to avoid Resource.LoadAll would be to do something like: using System The code for that would look something like this: // The name of the method does not matter. (The script does not need to be in the scene) If you really want to call a method on a ScriptableObject when the game starts, consider using the attribute - Allow a runtime class method to be initialized when a game is loaded at runtime without action from the user. Instead of having the scriptable objects add themselfs, consider having the factory call Resources.LoadAll this will load all the objects of type T in your project. 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. And it’s working fine.My factory creates instances based off ScriptableObject's ("SO") properties. This way I can interact and access the Player object in both scenes. Public class PlayerEnable : MonoBehaviour Public class LoadSceneOnClick : MonoBehaviourĪnd this script is attached to an empty GameObject in the other scene The Space Station: using System.Collections This script is attached to a button on the main menu scene: using System.Collections In the main menu I have a button and I did that when I click on the button it will activate the Player and will unload the main menu scene: Now when running the game it start with the main menu scene and the main menu camera. If the Player will be on enabled true I will see the Player camera and the scene The Space Station will be the first one when running the game. I also have another scene name Main Menu when I’m running the game I want it to start with the Main Menu scene and the also the Main Menu scene main camera. In the first one the The Space Station scene I have a object name Player.
0 Comments
Read More
Leave a Reply. |