Skip to content
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

FromComponentInParent injection fails when component is inactive #275

Closed
taylank opened this issue May 27, 2017 · 3 comments
Closed

FromComponentInParent injection fails when component is inactive #275

taylank opened this issue May 27, 2017 · 3 comments

Comments

@taylank
Copy link

taylank commented May 27, 2017

I noticed that when I instantiate a new prefab with the IInstantiator.InstantiatePrefabResourceForComponent method using a custom factory, the prefab instance generated starts out as inactive. Because of that, any members on the children of the prefab using FromComponentInParent binding for injection tend to fail.

var res = ((MonoBehaviour)ctx.ObjectInstance).GetComponentsInParent<TContract>() .Where(x => !ReferenceEquals(x, ctx.ObjectInstance));

MonoBehaviour.GetComponentsInParent ignores inactive behaviours by default, although this can be avoided with the (bool includeInactive = true) flag.

Setting that flag to true makes things work just fine for me, but I was wondering if the current behaviour of that zenject binding is deliberate, and if yes, what the reasoning is.

@taylank taylank changed the title FromComponentInParent injection fails when component in inactive FromComponentInParent injection fails when component is inactive May 27, 2017
@ryanwiesjahn
Copy link

Running into the same issue. I couldn't figure out why FromComponentInParents wasn't working sometimes until I found this. @svermeulen, any plans to add includeInactive to FromBinderGeneric<>.FromComponentInParents?

@svermeulen
Copy link

The reason that it's inactive during injection is because we have to do it that way to ensure that injection happens before awake. It's tempting to change it to have includeInactive set to true as the default, but then that would be the opposite of what unity does with GetComponentsInChildren. It's not a great situation but not sure how to address. For now I think you just have to pass includeInactive as true every time you use the FromComponentX methods to ensure it works for prefabs. @taylank added it for FromComponentInParents already

svermeulen added a commit that referenced this issue Jun 5, 2018
@svermeulen
Copy link

svermeulen commented Jun 5, 2018

I changed my mind about it. Having includeInactive set to false as a default makes this behaviour happen frequently for new users. So I've change the default to true in zenject 6 even though it's possible that will break things for some people upgrading. I think this issue is common enough to justify it. It is the opposite default of unity's GetComponentX methods, but makes more sense to both address this issue and also just in general. We already inject into all inactive components, so kind of makes sense to also resolve inactive components via the FromComponentX methods by default as well

starikcetin referenced this issue in starikcetin/Extenject Aug 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants