![]() Without it, you do not have the ability to drag items in, or edit it from the inspector, at all. This is important, as it tells Unity to serialise the struct, allowing it to display the fields in the inspector. In this example, we have an array of elements that each store a GameObject reference and a string. Expose the struct inside the Unity Inspector A text-based tag for easier searching, for example. You might prefer to create your own structure, in order to store extra values with your prefab reference. You can easily reference the elements in the array by their index, using prefabArray, prefabArray and prefabArray to access the first, second and third prefabs listed in the array. ![]() Most importantly, arrays store the elements in a more generic format. There are always ways around that, but it sounds like you may not need to, in the first place. As I have always been taught, C# is particularly troublesome with increasing the maximum size of an array, past the initial instantiation. public GameObject prefabArray Ī more generic array has its limitations. It sounds like all objects would be referenced before the game is run, so you will probably be better off with an array. public List prefabList Ī list allows greater manipulation functionality, and are more suitable when you intend to add more items to the list past the initial instantiation. You can create a simple GameObject array to store your references. There are several approaches you can take for this, two of which I will mention: Arrays or Lists There will likely be a specific method used to create these static classes, throughout your project, as you will often find need to do so.įrom here, all you need to do is create an interface to allow you to store your prefab references through the Unity interface. If you are working within a team, discuss other options with them. In XNA, I have used services to the same effect, but can not comment on how they relate to or work in Unity. ![]() Though I am still fairly new to the concept, I am told that Singletons can be helpful in this area. There are other, and far better, ways to approach creating a central 'control script'. You have to ensure that you only have one instance running, for it to work correctly, but it can be a simple and helpful workaround. In this example, I can access my PrefabManager script from any other script, using the line ntrol. Public static PrefabManager control // cheeky self-referenceĬontrol = this // linking the self-reference Public class PrefabManager: MonoBehaviour I tend to favor the static self-reference approach, as I work in a very controlled environment. The first step is to set up a global controller script. This script can also handle any other management functionality you might need, in regards to prefabs, now or in the future. If you would prefer the "drag and drop" approach of being able to drag your prefabs in from the inspector, there is an easy work around to working with a script that is not inherited from MonoBehaviour create a separate MonoBehaviour to work as the go-between. My preferred method to get around this is to add a reference to the object at run-time.Create a central-control script to manage your prefabs A way to think about this is: What would your game do if it instantiated the prefab in a scene without that other asset, even though you linked it in the prefab? "Well, I'm not going to do that though" is not a good excuse to the Unity Editor, for what reason I don't know. I think the problem is that the prefab can not see instantiated objects that are already in the scene.
0 Comments
Leave a Reply. |