Search Unity

Help with Singleton Toolbox pattern implementation

Discussion in 'Scripting' started by rhysp, Jun 14, 2016.

  1. rhysp

    rhysp

    Joined:
    Jun 14, 2016
    Posts:
    7
    I have been reading http://wiki.unity3d.com/index.php/Toolbox to try implement a better Singleton pattern

    I have created the necessary classes (MonoBehaviourExtended, Singleton, Toolbox), and can make use of this with the sample MyClass, only if the "(optional) runtime registration of global objects" section is removed.

    With that code in place as per the example, I will receive the following error:
    Code (csharp):
    1. Assets/TestManager.cs(24,60): error CS0309: The type `MyComponent' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method `Toolbox.RegisterComponent<T>()'
    Can anybody suggest how this should work? Any help is appreciated
     
  2. rhysp

    rhysp

    Joined:
    Jun 14, 2016
    Posts:
    7
    I seem to have resolved this through much trial and error. The issue I had was the example implementation writes:

    Code (csharp):
    1. MyComponent myComponent = Toolbox.Instance.RegisterComponent<MyComponent>();
    But it worked once I changed this too (removing the 'Instance' request):

    Code (csharp):
    1. MyComponent myComponent = Toolbox.RegisterComponent<MyComponent>();