Skip to content

Find Parent NullReference #830

@Chenyichen12

Description

@Chenyichen12

When I write the following code

    public class AutoParentScope : LifetimeScope
    {
        public LifetimeScope parentScope;

        protected override LifetimeScope FindParent()
        {
            var parentObj = transform.parent.gameObject;
            if (parentScope == null)
            {
                return parentObj.GetComponentInParent<LifetimeScope>();
            }
            else
            {
                return parentScope;
            }
        }
    }

Then I get the error message

System.NullReferenceException: Object reference not set to an instance of an object
at VContainer.Unity.LifetimeScope.Build () [0x0006e] in ./Library/PackageCache/jp.hadashikick.vcontainer@0cb993cc4615/Runtime/Unity/LifetimeScope.cs:199
at VContainer.Unity.LifetimeScope.Awake () [0x0004f] in ./Library/PackageCache/jp.hadashikick.vcontainer@0cb993cc4615/Runtime/Unity/LifetimeScope.cs:145

It seem likes the parent's container is not builded. Then it uses parent container

// ReSharper disable once PossibleNullReferenceException
Parent.Container.CreateScope(builder =>
{
    builder.RegisterBuildCallback(SetContainer);
    builder.ApplicationOrigin = this;
    builder.Diagnostics = VContainerSettings.DiagnosticsEnabled ? DiagnositcsContext.GetCollector(scopeName) : null;
    InstallTo(builder);
});

I just want to drag one scope in inspector and make it as child. Is it not allowed ?

Version 1.17.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions