I have a question about the error that occurs when I add a Generic-based class to a Component as a Singleton in unityA generic error occurred in GDI+, JPEG Image to MemoryStreamWhy is my namespace not recognized in Visual Studio / xamlUnit testing in case of a service locator patternCompilation Error -The call is ambiguous between the following methods or propertiesCreate Android plugin with Unity (C#)How to make Unity singleton base class to support generics?Singleton pattern by using UnityWandering AI in unity C#how to create a generic singleton class in Unity?Create Event Manager (Messaging System) in Unity Using delegate and event
Why would one crossvalidate the random state number?
In "Avengers: Endgame", what does this name refer to?
What does にとり mean?
Why didn't this character get a funeral at the end of Avengers: Endgame?
How can a hefty sand storm happen in a thin atmosphere like Martian?
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
Piano: quaver triplets in RH v dotted quaver and semiquaver in LH
Is it normal for gliders not to have attitude indicators?
Why is my arithmetic with a long long int behaving this way?
Where are the "shires" in the UK?
Sci-fi/fantasy book - ships on steel runners skating across ice sheets
Why does blending blueberries, milk, banana and vanilla extract cause the mixture to have a yogurty consistency?
Is there an age requirement to play in Adventurers League?
Why does sound not move through a wall?
How to pass hash as password to ssh server
How to pass query parameters in URL in Salesforce Summer 19 Release?
Should I simplify my writing in a foreign country?
Disabling quote conversion in docstrings
weird pluperfect subjunctive in Eutropius
Why did WWI include Japan?
Krull dimension of the ring of global sections
Sparring against two opponents test
All superlinear runtime algorithms are asymptotically equivalent to convex function?
Which US defense organization would respond to an invasion like this?
I have a question about the error that occurs when I add a Generic-based class to a Component as a Singleton in unity
A generic error occurred in GDI+, JPEG Image to MemoryStreamWhy is my namespace not recognized in Visual Studio / xamlUnit testing in case of a service locator patternCompilation Error -The call is ambiguous between the following methods or propertiesCreate Android plugin with Unity (C#)How to make Unity singleton base class to support generics?Singleton pattern by using UnityWandering AI in unity C#how to create a generic singleton class in Unity?Create Event Manager (Messaging System) in Unity Using delegate and event
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
- situation
I want to implement a generic-based 'PoolingManagerBase' in a singleton pattern for class pooling.
- Question
Why is it that a class that inherits from 'PoolingManagerBase' does not create a component that contains a singleton instance?
- Configuration
First, I implement the generic-based 'Singleton' class.
The 'Singleton' class code looks like this
public abstract class Singleton<T> : CComponent where T : CComponent
private static T _Instance = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent<T>();
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create() => Instance;
Items are also held in the form of 'PoolingItemBase' and scoped to ITEM as below
public class PoolingItemBase where ITEM : PoolingItemBase<ITEM>
Therefore, the item manager also scopes ITEM as below
public class PoolingManagerBase <ITEM>: Singleton <PoolingManagerBase<ITEM>> where ITEM : PoolingItemBase<ITEM>
- apply
To actually use it, we construct the child class as shown below.
public class TempItem : PoolingItemBase<TempItem>
public class TempItemManager : PoolingManagerBase<TempItem>
- result
Singleton's
_Instance = gObject.AddComponent<T>();
Null is returned in part
I've been shoveling for two days, but I do not have a pointy reason ...
Is there a limit to writing nested generics in Unity?
--------------- EDIT ---------------
To distinguish it from the existing Unity Component, I replaced the Singleton inherited Component with CComponent
Below is the code for the CComponent class
public abstract class CComponent : MonoBehaviour
[HideInInspector] public Transform _Transform = null;
[HideInInspector] public Rigidbody _Rigidbody = null;
[HideInInspector] public Rigidbody2D _Rigidbody2D = null;
public virtual void Awake()
_Transform = transform;
_Rigidbody = GetComponentInChildren<Rigidbody>();
_Rigidbody2D = GetComponentInChildren<Rigidbody2D>();
public virtual void Update()
public virtual void FixedUpdate()
public virtual void LateUpdate()
- It seems to have come to an answer after thinking about the painful time.
When I added_Instance
viaAddComponent<T>()
insideSingleton
, the type I actually imported came with information about<T>
, not the lowest class.
We just learned that Unity does not support generic-based components, so the above code did not work.
So I modified theSingleton
with the following code.
public abstract class Singleton<T> : CComponent where T : MonoBehaviour
private static T _Instance = null;
private static Type _InstanceType = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent(_InstanceType) as T;
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create(Type tType)
_InstanceType = tType;
return Instance;
I had the inconvenience of going through Create(typeof(T))
, but now it's working well.
c# unity3d generics
|
show 9 more comments
- situation
I want to implement a generic-based 'PoolingManagerBase' in a singleton pattern for class pooling.
- Question
Why is it that a class that inherits from 'PoolingManagerBase' does not create a component that contains a singleton instance?
- Configuration
First, I implement the generic-based 'Singleton' class.
The 'Singleton' class code looks like this
public abstract class Singleton<T> : CComponent where T : CComponent
private static T _Instance = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent<T>();
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create() => Instance;
Items are also held in the form of 'PoolingItemBase' and scoped to ITEM as below
public class PoolingItemBase where ITEM : PoolingItemBase<ITEM>
Therefore, the item manager also scopes ITEM as below
public class PoolingManagerBase <ITEM>: Singleton <PoolingManagerBase<ITEM>> where ITEM : PoolingItemBase<ITEM>
- apply
To actually use it, we construct the child class as shown below.
public class TempItem : PoolingItemBase<TempItem>
public class TempItemManager : PoolingManagerBase<TempItem>
- result
Singleton's
_Instance = gObject.AddComponent<T>();
Null is returned in part
I've been shoveling for two days, but I do not have a pointy reason ...
Is there a limit to writing nested generics in Unity?
--------------- EDIT ---------------
To distinguish it from the existing Unity Component, I replaced the Singleton inherited Component with CComponent
Below is the code for the CComponent class
public abstract class CComponent : MonoBehaviour
[HideInInspector] public Transform _Transform = null;
[HideInInspector] public Rigidbody _Rigidbody = null;
[HideInInspector] public Rigidbody2D _Rigidbody2D = null;
public virtual void Awake()
_Transform = transform;
_Rigidbody = GetComponentInChildren<Rigidbody>();
_Rigidbody2D = GetComponentInChildren<Rigidbody2D>();
public virtual void Update()
public virtual void FixedUpdate()
public virtual void LateUpdate()
- It seems to have come to an answer after thinking about the painful time.
When I added_Instance
viaAddComponent<T>()
insideSingleton
, the type I actually imported came with information about<T>
, not the lowest class.
We just learned that Unity does not support generic-based components, so the above code did not work.
So I modified theSingleton
with the following code.
public abstract class Singleton<T> : CComponent where T : MonoBehaviour
private static T _Instance = null;
private static Type _InstanceType = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent(_InstanceType) as T;
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create(Type tType)
_InstanceType = tType;
return Instance;
I had the inconvenience of going through Create(typeof(T))
, but now it's working well.
c# unity3d generics
Sorry... I am Korean and I use translator so I can use poor English
– Undouble
Mar 22 at 9:19
Any errors occurs while following that procedure?
– Morasiu
Mar 22 at 9:33
2
Only scripts the derive fromMonoBevahiour
can be added as components to a GameObject. Does your ClassSingleton
inherit fromMonoBehaviour
anywhere?
– remy_rm
Mar 22 at 9:40
1
@Undouble I;m not quite sure how this code would work your version ofCComponent
does not appear to inherit fromMonoBehavior
orComponent
theAddComponent
functionality expects a class that inherits fromComponent
. In your use case I do not see a class that inherits from Unity'sComponent
. Can you show us your definition ofCComponent
?
– Eddge
Mar 22 at 12:14
1
@Undouble unfortunately I cannot help any further without an MCVE, having an MCVE will allow me to get an accurate idea of what you are trying to do(And the arrangements of your objects), and will allow me to provide more insight specific to the problem you are running into.
– Eddge
Mar 22 at 13:14
|
show 9 more comments
- situation
I want to implement a generic-based 'PoolingManagerBase' in a singleton pattern for class pooling.
- Question
Why is it that a class that inherits from 'PoolingManagerBase' does not create a component that contains a singleton instance?
- Configuration
First, I implement the generic-based 'Singleton' class.
The 'Singleton' class code looks like this
public abstract class Singleton<T> : CComponent where T : CComponent
private static T _Instance = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent<T>();
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create() => Instance;
Items are also held in the form of 'PoolingItemBase' and scoped to ITEM as below
public class PoolingItemBase where ITEM : PoolingItemBase<ITEM>
Therefore, the item manager also scopes ITEM as below
public class PoolingManagerBase <ITEM>: Singleton <PoolingManagerBase<ITEM>> where ITEM : PoolingItemBase<ITEM>
- apply
To actually use it, we construct the child class as shown below.
public class TempItem : PoolingItemBase<TempItem>
public class TempItemManager : PoolingManagerBase<TempItem>
- result
Singleton's
_Instance = gObject.AddComponent<T>();
Null is returned in part
I've been shoveling for two days, but I do not have a pointy reason ...
Is there a limit to writing nested generics in Unity?
--------------- EDIT ---------------
To distinguish it from the existing Unity Component, I replaced the Singleton inherited Component with CComponent
Below is the code for the CComponent class
public abstract class CComponent : MonoBehaviour
[HideInInspector] public Transform _Transform = null;
[HideInInspector] public Rigidbody _Rigidbody = null;
[HideInInspector] public Rigidbody2D _Rigidbody2D = null;
public virtual void Awake()
_Transform = transform;
_Rigidbody = GetComponentInChildren<Rigidbody>();
_Rigidbody2D = GetComponentInChildren<Rigidbody2D>();
public virtual void Update()
public virtual void FixedUpdate()
public virtual void LateUpdate()
- It seems to have come to an answer after thinking about the painful time.
When I added_Instance
viaAddComponent<T>()
insideSingleton
, the type I actually imported came with information about<T>
, not the lowest class.
We just learned that Unity does not support generic-based components, so the above code did not work.
So I modified theSingleton
with the following code.
public abstract class Singleton<T> : CComponent where T : MonoBehaviour
private static T _Instance = null;
private static Type _InstanceType = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent(_InstanceType) as T;
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create(Type tType)
_InstanceType = tType;
return Instance;
I had the inconvenience of going through Create(typeof(T))
, but now it's working well.
c# unity3d generics
- situation
I want to implement a generic-based 'PoolingManagerBase' in a singleton pattern for class pooling.
- Question
Why is it that a class that inherits from 'PoolingManagerBase' does not create a component that contains a singleton instance?
- Configuration
First, I implement the generic-based 'Singleton' class.
The 'Singleton' class code looks like this
public abstract class Singleton<T> : CComponent where T : CComponent
private static T _Instance = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent<T>();
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create() => Instance;
Items are also held in the form of 'PoolingItemBase' and scoped to ITEM as below
public class PoolingItemBase where ITEM : PoolingItemBase<ITEM>
Therefore, the item manager also scopes ITEM as below
public class PoolingManagerBase <ITEM>: Singleton <PoolingManagerBase<ITEM>> where ITEM : PoolingItemBase<ITEM>
- apply
To actually use it, we construct the child class as shown below.
public class TempItem : PoolingItemBase<TempItem>
public class TempItemManager : PoolingManagerBase<TempItem>
- result
Singleton's
_Instance = gObject.AddComponent<T>();
Null is returned in part
I've been shoveling for two days, but I do not have a pointy reason ...
Is there a limit to writing nested generics in Unity?
--------------- EDIT ---------------
To distinguish it from the existing Unity Component, I replaced the Singleton inherited Component with CComponent
Below is the code for the CComponent class
public abstract class CComponent : MonoBehaviour
[HideInInspector] public Transform _Transform = null;
[HideInInspector] public Rigidbody _Rigidbody = null;
[HideInInspector] public Rigidbody2D _Rigidbody2D = null;
public virtual void Awake()
_Transform = transform;
_Rigidbody = GetComponentInChildren<Rigidbody>();
_Rigidbody2D = GetComponentInChildren<Rigidbody2D>();
public virtual void Update()
public virtual void FixedUpdate()
public virtual void LateUpdate()
- It seems to have come to an answer after thinking about the painful time.
When I added_Instance
viaAddComponent<T>()
insideSingleton
, the type I actually imported came with information about<T>
, not the lowest class.
We just learned that Unity does not support generic-based components, so the above code did not work.
So I modified theSingleton
with the following code.
public abstract class Singleton<T> : CComponent where T : MonoBehaviour
private static T _Instance = null;
private static Type _InstanceType = null;
public static T Instance
get
if (_Instance == null)
GameObject gObject = new GameObject(typeof(T).ToString());
_Instance = gObject.AddComponent(_InstanceType) as T;
DontDestroyOnLoad(gObject);
return _Instance;
public static T Create(Type tType)
_InstanceType = tType;
return Instance;
I had the inconvenience of going through Create(typeof(T))
, but now it's working well.
c# unity3d generics
c# unity3d generics
edited Mar 23 at 2:29
Undouble
asked Mar 22 at 9:15
UndoubleUndouble
112
112
Sorry... I am Korean and I use translator so I can use poor English
– Undouble
Mar 22 at 9:19
Any errors occurs while following that procedure?
– Morasiu
Mar 22 at 9:33
2
Only scripts the derive fromMonoBevahiour
can be added as components to a GameObject. Does your ClassSingleton
inherit fromMonoBehaviour
anywhere?
– remy_rm
Mar 22 at 9:40
1
@Undouble I;m not quite sure how this code would work your version ofCComponent
does not appear to inherit fromMonoBehavior
orComponent
theAddComponent
functionality expects a class that inherits fromComponent
. In your use case I do not see a class that inherits from Unity'sComponent
. Can you show us your definition ofCComponent
?
– Eddge
Mar 22 at 12:14
1
@Undouble unfortunately I cannot help any further without an MCVE, having an MCVE will allow me to get an accurate idea of what you are trying to do(And the arrangements of your objects), and will allow me to provide more insight specific to the problem you are running into.
– Eddge
Mar 22 at 13:14
|
show 9 more comments
Sorry... I am Korean and I use translator so I can use poor English
– Undouble
Mar 22 at 9:19
Any errors occurs while following that procedure?
– Morasiu
Mar 22 at 9:33
2
Only scripts the derive fromMonoBevahiour
can be added as components to a GameObject. Does your ClassSingleton
inherit fromMonoBehaviour
anywhere?
– remy_rm
Mar 22 at 9:40
1
@Undouble I;m not quite sure how this code would work your version ofCComponent
does not appear to inherit fromMonoBehavior
orComponent
theAddComponent
functionality expects a class that inherits fromComponent
. In your use case I do not see a class that inherits from Unity'sComponent
. Can you show us your definition ofCComponent
?
– Eddge
Mar 22 at 12:14
1
@Undouble unfortunately I cannot help any further without an MCVE, having an MCVE will allow me to get an accurate idea of what you are trying to do(And the arrangements of your objects), and will allow me to provide more insight specific to the problem you are running into.
– Eddge
Mar 22 at 13:14
Sorry... I am Korean and I use translator so I can use poor English
– Undouble
Mar 22 at 9:19
Sorry... I am Korean and I use translator so I can use poor English
– Undouble
Mar 22 at 9:19
Any errors occurs while following that procedure?
– Morasiu
Mar 22 at 9:33
Any errors occurs while following that procedure?
– Morasiu
Mar 22 at 9:33
2
2
Only scripts the derive from
MonoBevahiour
can be added as components to a GameObject. Does your Class Singleton
inherit from MonoBehaviour
anywhere?– remy_rm
Mar 22 at 9:40
Only scripts the derive from
MonoBevahiour
can be added as components to a GameObject. Does your Class Singleton
inherit from MonoBehaviour
anywhere?– remy_rm
Mar 22 at 9:40
1
1
@Undouble I;m not quite sure how this code would work your version of
CComponent
does not appear to inherit from MonoBehavior
or Component
the AddComponent
functionality expects a class that inherits from Component
. In your use case I do not see a class that inherits from Unity's Component
. Can you show us your definition of CComponent
?– Eddge
Mar 22 at 12:14
@Undouble I;m not quite sure how this code would work your version of
CComponent
does not appear to inherit from MonoBehavior
or Component
the AddComponent
functionality expects a class that inherits from Component
. In your use case I do not see a class that inherits from Unity's Component
. Can you show us your definition of CComponent
?– Eddge
Mar 22 at 12:14
1
1
@Undouble unfortunately I cannot help any further without an MCVE, having an MCVE will allow me to get an accurate idea of what you are trying to do(And the arrangements of your objects), and will allow me to provide more insight specific to the problem you are running into.
– Eddge
Mar 22 at 13:14
@Undouble unfortunately I cannot help any further without an MCVE, having an MCVE will allow me to get an accurate idea of what you are trying to do(And the arrangements of your objects), and will allow me to provide more insight specific to the problem you are running into.
– Eddge
Mar 22 at 13:14
|
show 9 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296300%2fi-have-a-question-about-the-error-that-occurs-when-i-add-a-generic-based-class-t%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55296300%2fi-have-a-question-about-the-error-that-occurs-when-i-add-a-generic-based-class-t%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Sorry... I am Korean and I use translator so I can use poor English
– Undouble
Mar 22 at 9:19
Any errors occurs while following that procedure?
– Morasiu
Mar 22 at 9:33
2
Only scripts the derive from
MonoBevahiour
can be added as components to a GameObject. Does your ClassSingleton
inherit fromMonoBehaviour
anywhere?– remy_rm
Mar 22 at 9:40
1
@Undouble I;m not quite sure how this code would work your version of
CComponent
does not appear to inherit fromMonoBehavior
orComponent
theAddComponent
functionality expects a class that inherits fromComponent
. In your use case I do not see a class that inherits from Unity'sComponent
. Can you show us your definition ofCComponent
?– Eddge
Mar 22 at 12:14
1
@Undouble unfortunately I cannot help any further without an MCVE, having an MCVE will allow me to get an accurate idea of what you are trying to do(And the arrangements of your objects), and will allow me to provide more insight specific to the problem you are running into.
– Eddge
Mar 22 at 13:14