#Error when adding a prefab to another prefab

1 messages · Page 1 of 1 (latest)

fair urchin
#

I get this error when building but not in the Editor in AssetImportWorker0:
InvalidOperationException: Entity Entity(20:1) doesn't belong to the current authoring component. GameObject 'TestEnemyAuthoring' in Scene
Here's the authoring script:

using UnityEngine;
using Unity.Entities;

public class TestPrefabAuthoring : MonoBehaviour
{
    public GameObject TestPrefab;
}

public class TestPrefabBaker : Baker<TestPrefabAuthoring>
{
    public override void Bake(TestPrefabAuthoring authoring)
    {
        Entity entity = GetEntity(TransformUsageFlags.Dynamic);
        var prefab = GetEntity(authoring.TestPrefab, TransformUsageFlags.Dynamic);

        AddComponent(entity, new TestComponent
        {
            TestEntity = prefab
        });
    }
}```
Here's the component script:
```cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;

public struct TestComponent : IComponentData
{
    public Entity TestEntity;
}

Here's the prefab and the player in subscene that's referencing it to use it:

heady oak
#

TestPrefabAuthoring/TestPrefabBaker looks fine i think

#

but the error code is in your TestEnemyAuthoring script

#

not your TestPrefabAuthoring

fair urchin
#

TestEnemyAuthoring is:

using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;

public class TestEnemyAuthoring : MonoBehaviour
{
    public GameObject BulletPrefab;
}

public class EnemyBaker : Baker<TestEnemyAuthoring>
{
    public override void Bake(TestEnemyAuthoring authoring)
    {

        var bulletPrefab = GetEntity(authoring.BulletPrefab, TransformUsageFlags.Dynamic);

        Entity enemyEntity = GetEntity(TransformUsageFlags.Dynamic);
        //var enemyEntity = CreateAdditionalEntity(TransformUsageFlags.Dynamic);

        AddComponent(enemyEntity, new TestEnemyBulletsComponent
        {
            BulletPrefab = bulletPrefab
            //BulletPrefab = authoring.BulletPrefab
        });
    }
}```
#

its just the main piece on working on, I made TestPrefabAuthoring to isolate the issue

#

but they're essentially interchangeable here as its the same duplicate prefab and very similar authoring components

#

I swapped my project back to the one erroring out and no there isn't.

On the TestEnemy prefab, there's a transform, sprite renderer (just a square), a box collider, a rigidbody, and the TestEnemyAuthoring script

#

This is my scene, pretty barebones. Ignore the 2 camera thing, that should be unrelated

heady oak
#
        AddComponent(enemyEntity, new TestEnemyBulletsComponent
        {
            BulletPrefab = bulletPrefab
            //BulletPrefab = authoring.BulletPrefab
        });```
#

this isn't allowed

#

you're adding a component to an entity that isn't owned by this authoring

fair urchin
#

What does that baker own then?

heady oak
#

oh wait i'm wrong sorry

#

you just inverted how it was written

fair urchin
#

yeah

#

mb lol

heady oak
#

it's easier to read if you add highlighting

fair urchin
#

c# in the code block?

heady oak
#

triple `
then cs

#
using System.Collections;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;

public class TestEnemyAuthoring : MonoBehaviour
{
    public GameObject BulletPrefab;
}

public class EnemyBaker : Baker<TestEnemyAuthoring>
{
    public override void Bake(TestEnemyAuthoring authoring)
    {

        var bulletPrefab = GetEntity(authoring.BulletPrefab, TransformUsageFlags.Dynamic);

        Entity enemyEntity = GetEntity(TransformUsageFlags.Dynamic);
        //var enemyEntity = CreateAdditionalEntity(TransformUsageFlags.Dynamic);

        AddComponent(enemyEntity, new TestEnemyBulletsComponent
        {
            BulletPrefab = bulletPrefab
            //BulletPrefab = authoring.BulletPrefab
        });
    }
}```
fair urchin
#

ah i see

#

Should be fixed for the original post

#

QQ, is this method I'm doing supposed to theoretically work on the prefab itself? Cause the only other method I could find that was almost as straightforward was to make a copy of the prefab in the scene and add the authoring there, but that kinda sucks compared to on the prefab

heady oak
#

it looks fine

#

this is pretty basic behaviour

#

something else is going on

#

i copy / pasted your code you posted here and it's fine

fair urchin
#

wait is it supposed to go in the subscene or on the prefab

#

If its in the subscene for me it bakes the prefab component onto the authoring script entity instead of the TestEnemy prefab

lyric heron
#

Prefab should be in the assets folder, the component that's not a prefab should be on a gameobject in the subscene, and the second should refer to the first

fair urchin
#

Doesn't this just make the gameobject in the subscene into an entity that stores a reference to a prefab?

Is there no way of easily adding components to a prefab in an authoring-baking script? It doesn't make sense for my use case to store a reference to another entity, as it makes things harder to iterate over.

My prefab is located in Assets/somefolder/ which should be adequate. I did it get working using your approach, but the data layout just seems bad to work with. I'd like to essentially create a prefab with some components and some values initialized within those components, and ideally with those values editable in the inspector from the prefab.

#

What I'm currently doing which isn't great is making a prefab instance in the subscene, attaching the authoring but making sure it gets the Prefab tag so it doesn't interact in queries. Other prefab instances in the subscene don't get the prefab tag.

This comes with the issue that whenever I need to change the values of the components, I need to change each one in the scene one by one rather than them all inheriting values from the prefab by default.

lethal flint
#

No, what you are doing is pretty common and trivial. The issue is I don't get why it doesn't work for you. I have the same setup in 3 projects and they just work.

lethal flint
#

I've just tested this setup using Unity 2022.3.39, Entities 1.2.4.

fair urchin
#

im using the same entities version but on 43

lethal flint
#

This is my setup in details

fair urchin
#

Thanks, that's really good documentation. I'll try to replicate

fair urchin
#

Interesting, that doesn't build on my setup

#

So I guess something wrong with the Unity installation

#
UnityEditor.Experimental.AssetDatabaseExperimental:GetArtifactPaths (UnityEditor.Experimental.ArtifactID,string[]&)
Unity.Scenes.Editor.EntitySceneBuildUtility:PrepareEntityBinaryArtifacts (Unity.Entities.Hash128,System.Collections.Generic.HashSet`1<Unity.Entities.Hash128>,System.Collections.Generic.Dictionary`2<Unity.Entities.Hash128, UnityEditor.Experimental.ArtifactKey>) (at ./Library/PackageCache/com.unity.entities@1.2.4/Unity.Scenes.Editor/EntitySceneBuildUtility.cs:70)
Unity.Scenes.Editor.EntitySceneBuildPlayerProcessor:PrepareForBuild (UnityEditor.Build.BuildPlayerContext) (at ./Library/PackageCache/com.unity.entities@1.2.4/Unity.Scenes.Editor/EntitySceneBuildPlayerProcessor.cs:246)
UnityEditor.EditorApplication:Internal_CallGlobalEventHandler ()
lethal flint
#

Beside the main Entities package, do you install Graphics and Physics too?

fair urchin
#

yep

lethal flint
#

Let try downgrading Unity to .39 then 🤔

fair urchin
#

Yeah that's my thinking, I'll try again tomorrow and install .39 overnight

lyric heron
# fair urchin Doesn't this just make the gameobject in the subscene into an entity that stores...

I'm confused what you're trying to do.

the data layout just seems bad to work with
Can you elaborate on what you mean by this?

I'd like to essentially create a prefab with some components and some values initialized within those components, and ideally with those values editable in the inspector from the prefab.
Isn't this exactly what you can do by having it in the asset folder? Or do you mean you want to edit them at runtime?

fair urchin
#

I just wanted to do what Green Leaf's repo is doing

#

An enemy prefab with some stuff attached to it, and some components on it, where the component values are set in the inspector.

I'd like to then attach this (in a component) to a player prefab or gameobject in the subscene which will get converted into an entity

#

Isn't this exactly what you can do by having it in the asset folder? Or do you mean you want to edit them at runtime?
No, this errors for me if I have any authoring+baking script on my prefab in Assets and has a dependency in a subscene so it actually gets baked. It works if I don't have an authoring script on the prefab though but then obviously I can't attach any components to the prefabs using the simple method.

#

I think there might be some weird things going on in my installation as I noticed that SystemAPI.GetSingletonEntity runs but causes a lot of build errors every frame, but using SystemAPI.TryGetSingletonEntity is perfectly fine, which doesn't exactly seem right.

fair urchin
#

Thanks Green Leaf, got it working now on a fresh installation of 39. Didn't have to change anything in my code for my project so it looks like a transient issue with installing 43.

My thanks to everyone that provided their feedback here.

lethal flint
#

.45 has just showed up but doesn't include a statement for this issue.

fair urchin
#

I don't see the subscene baking issue in the .40 notes, is it mentioned there and I glossed over it?

lethal flint
#

But because some people has experienced it firsthand, we know there is an issue.