-
-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Intended behaviour of RegisterComponentInHierarchy() #727
Comments
I didn't really understand why this was happening. RegisterComponentInHierarchy is for searching for MonoBehaviours that have already been placed in the scene.
From the EnemyLifetimeScope name, |
Here is a small sample project that hopefully demonstrates the issue.
I'm finding that using scopes like that has advantages above a certain complexity of prefab. |
Ah oK. I understand the problem. I'll think about how to deal with it. |
Hi,
recently I was debugging an issue in our project related to multiple repeated injections into a MonoBehaviour, let's call it
InstancingSystem
. This system is registered in theSceneLifetimeScope
usingRegisterComponentInHierarchy<InstancingSystem>()
. Agents can use this system to get instances of pooled objects, for example projectiles. More complex agents, such as enemies, have their own nestedEnemyLifetimeScope
.The problem is the following:
InstancingSystem
is injected with aIObjectResolver
, initially theSceneLifetimeScope
's. Now, when an enemy is spawned, theIObjectResolver
is re-injected, this time with theEnemyLifetimeScope
's. When the enemy is destroyed, theIObjectResolver
is destroyed with it, resulting in NullReferenceExceptions whenInstancingSystem
is used again.The root of the issue is, at least in my opinion, the fact that
RegisterComponentInHierarchy()
usesLifetime.Scoped
. This does not make sense to me, because in my understandingScoped
is supposed to ensure that a new instance is created for each scope. This is obviously not the case for Components and a automatic re-injection should also always be avoided anyway IMO.In general I can't really think of a use case where the behavior of
Lifetime.Scoped
is preferable toLifetime.Singleton
when usingRegisterComponentInHierarchy()
.I'm also not the only one who has run into issues with this behavior, see #709 and #704 for examples.
Yes, I can just use
RegisterComponent(FindFirstObjectOfType<InstancingSystem>())
to work around this, but then what is the point ofRegisterComponentInHierarchy()
?The text was updated successfully, but these errors were encountered: