New answers tagged

1

I'll throw my singleton into the mix. It checks off all four of DMGregory's criteria (see his answer). This has a lot of stuff that may not make sense. It is just covering Unity specific corner cases, and persists between scenes. It is tried and tested, and the most current version can be found on my Github. Enjoy! Usage is by having your script extend this ...


0

Add [DisallowMultipleComponent]before your class to make sure you can't add more than one instance of it. example: [DisallowMultipleComponent] public class Toolbox : MonoBehaviour Also, I recommend reading DMGregory's comment before implementing this, it might be better to just create a static class instead.


2

Another option might be to split the class into two parts: a regular static class for the Singleton component, and a MonoBehaviour that acts as a controller for the singleton instance. This way you have full control over the singleton's construction, and it will persist across scenes. This also lets you add controllers to any object that might need the ...


2

I'd just like to add that it may be useful to call DontDestroyOnLoad if you want your singleton to persist across scenes. public class Singleton : MonoBehavior { private static Singleton _instance; public static Singleton Instance { get { return _instance; } } private void Awake() { if (_instance != null ...


7

Here's a quick summary: Create object Removes scene Global Keep across if not in scene? duplicates? access? Scene loads? Method 1 No No Yes Yes Method 2 Yes No Yes No PearsonArtPhoto No Yes Yes ...


4

It depends, but usually I use a third method. The problem with the methods that you used is that in the event that the object is included to begin with, it will not remove them from the tree, and they can still be created by instantiating too many calls, which could make things really confusing. public class SomeClass : MonoBehavior { private static ...


1

I see no problem. Just declare a multitude of Fire functions each of which calls respective projectile class constructor. More code on the inside, but simplest interface on the outside (easy to use): ProjectileManager.FireA(param1, param2); ProjectileManager.FireB(param1, param2_typeA, param3); ProjectileManager.FireC(param1, param2_typeB, param3); ...



Top 50 recent answers are included