Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by shadowking · Aug 27, 2014 at 10:34 AM · singletongenerics

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
     }
 
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rhysp · Jun 18, 2016 at 06:25 PM 0
Share

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/)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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>();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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>();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Big problem with a small singleton 1 Answer

MonoBehaviour.OnDestroy() how it works/ how to use it? 1 Answer

MonoBehaviour Inheritance, Singletons and Generics 1 Answer

How to make an abstract pseudo-singleton class 1 Answer

Extending a Singleton base class (UJS) (?) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges