ssannandeji / Zenject-2019

Dependency Injection Framework for Unity3D
MIT License
2.53k stars 363 forks source link

context.ObjectInstance is null when bound to a factory #604

Open Ricimon opened 5 years ago

Ricimon commented 5 years ago

In the example for UnderTransform(Method) in the readme, it says that context.ObjectInstance can be checked and used as a potential parent for a newly created GameObject made from a factory. However, context.ObjectInstance is null when running the given example.

Code I tested with:

public class TestInstaller : MonoInstaller
{
    public GameObject fooPrefab;

    public override void InstallBindings()
    {
        Container.BindFactory<Foo, Foo.Factory>()
            .FromComponentInNewPrefab(fooPrefab)
            .UnderTransform(GetParent);
    }

    Transform GetParent(InjectContext context)
    {
        Debug.Log(context.ObjectInstance);
        if (context.ObjectInstance is Component)
        {
            return ((Component)context.ObjectInstance).transform;
        }
        return null;
    }
}
public class Foo : MonoBehaviour
{
    public class Factory : PlaceholderFactory<Foo> { }
}
public class FooSpawner : MonoBehaviour
{
    [Inject] readonly Foo.Factory fooFactory;

    void Start()
    {
        fooFactory.Create();
    }
}

There's also a typo in that section, FromComponentInNewGameObject is not a valid method (perhaps it should've been FromNewComponentOnNewGameObject?)