- Home /
Singleton Toolbox compile error
I'm trying to use the Toolbox Singleton implementation you can find here: http://wiki.unity3d.com/index.php/Toolbox
From my main game controller I'm trying to register my map loader component in the following way:
using UnityEngine;
using System.Collections;
public class MainGameController : MonoBehaviour {
void Awake(){
Screen.SetResolution (Screen.height, Screen.width, true);
MapLoader mLoader = Toolbox.Instance.RegisterComponent<MapLoader>();
}
}
But in the console window of unity I get the following compile error:
Assets/Scripts/MainGameController.cs(11,54): error CS0176: Static member `Toolbox.RegisterComponent()' cannot be accessed with an instance reference, qualify it with a type name instead
I'm not really experienced with unity game dev nor with C# generics, so I can't find what I'm doing wrong.
This is the map loader class
using UnityEngine;
using System.Collections;
[System.Serializable]
public class MapLoader {
private ArrayList tilesInstances = new ArrayList();
public ArrayList TilesInstances
{
get { return tilesInstances; }
}
private void LoadMap()
{
//Do some stuff
}
}
Was this ever resolved? I am experiencing the same issue (and posted a similar question here: http://forum.unity3d.com/threads/help-with-singleton-toolbox-pattern-implementation.411040/)
Answer by Bunny83 · Jun 18, 2016 at 07:01 PM
The error is quite clear what you did wrong. Just have a look at the declaration of the RegisterComponent method:
static public T RegisterComponent<T> () where T: Component {
return Instance.GetOrAddComponent<T>();
}
The method is a static method, not an instance method. So you just have to use:
MapLoader mLoader = Toolbox.RegisterComponent<MapLoader>();
As you can see the method RegisterComponent uses the "Instance" property internally to access the singleton instance. That method is just a shortcut for:
Toolbox.Instance.GetOrAddComponent<MapLoader>();
// is the same as:
//Toolbox.RegisterComponent<MapLoader>();
Answer by rhysp · Jun 19, 2016 at 04:53 AM
@shadowking I seem to have resolved this through much trial and error. The issue I had was the example implementation writes:
MyComponent myComponent = Toolbox.Instance.RegisterComponent<MyComponent>();
But it worked once I changed this too (removing the 'Instance' request):
MyComponent myComponent = Toolbox.RegisterComponent<MyComponent>();
Your answer
![](https://koobas.hobune.stream/wayback/20220613161637im_/https://answers.unity.com/themes/thub/images/avi.jpg)