Search Unity

Spawning the wrong prefab

Discussion in 'Entity Component System' started by andynayrb, Jan 24, 2020.

  1. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    So at startup I have a class that spawns a bunch of prefab tiles and blocks , in a checkerboard pattern. Then I have my character spawn a bullet when I hit the button on the mouse. I have a job to do this, I assign a bullet prefab, in the inspector, for the job to instantiate. When I test the game, the tiles and blocks spawn perfectly and my player is in position. I press fire and another tile is spawned, if I press fire again another tiles is spawned. I am guessing that I need to do something in the spawnTiles class to clear the tile prefab out of memory.

    I can provide code if needed but I assume somebody will just tell me I forgot to do something. I assume some container or collection, that I don't even know is a collection, needs to be cleared or disposed of.

    Thoughts would be appreciated.
     
  2. Curlyone

    Curlyone

    Joined:
    Mar 15, 2018
    Posts:
    41
    Yeah, better to post the code so people can understand and help better.
     
  3. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    This shows the important stuff I think. Thanks.

    Code (CSharp):
    1.  
    2.  
    3. public class SpawnTiles : MonoBehaviour
    4. {
    5.     public GameObject Prefab;
    6.     public GameObject Prefab2;
    7.     public GameObject Prefab3;
    8.     public GameObject Prefab4;
    9.     public int CountX = 16;
    10.     public int CountY = 16;
    11.     private int xPlace = -48;
    12.     private int yPlace = 4;
    13.     public GameObjectConversionSettings settings;
    14.     public EntityManager entityManager;
    15.     void Start()
    16.     {
    17.         // Create entity prefab from the game object hierarchy once
    18.         settings = GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, new BlobAssetStore());
    19.         var prefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab, settings);
    20.         var prefab2 = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab2, settings);
    21.         var prefab3 = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab3, settings);
    22.         var prefab4 = GameObjectConversionUtility.ConvertGameObjectHierarchy(Prefab4, settings);
    23.  
    24.        
    25.         entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    26.        
    27.         var last = true;
    28.         var random = new Unity.Mathematics.Random(0x6E624EB7u);
    29.         Entity instance;
    30.  
    31.         //create blocks from prefabs .  Just loop through them.  I cut the boring part out.
    32.  
    33.         }
    34.        
    35.     }
    36.  
    37.     void OnDestroy()
    38.     {
    39.         settings.BlobAssetStore.Dispose();
    40.        
    41.     }
    42. }
    43.  
     
  4. Curlyone

    Curlyone

    Joined:
    Mar 15, 2018
    Posts:
    41
    I think you posted the properly working code, i believe its better if you post your "spawn bullet on click" system because it seems thats where your issue is, and also if you post how do you assign that prefab to that system can also help.
     
  5. andynayrb

    andynayrb

    Joined:
    Feb 28, 2019
    Posts:
    25
    You are correct, I did post the wrong code. I was in a hurry. Anyhow, I deleted most of the code and redid it. It now spawns the correct prefab (hooray), however it causes my character to fall through the floor. And I get 7 errors, none of which say anything about where the error is. They are slice errors

    Slice start + length (13) range must be <= array.Length (12)

    I know when they happen, during the instantiation of the prefab. They are happening in the physics world. But I know I am sending them something they don't like. Just not sure what that is.

     
    [UpdateAfter(typeof(GunInputSystem))]
    public class GunShootingSystem : ComponentSystem
    {
    protected override void OnUpdate()
    {
    float dt = Time.fixedDeltaTime;
    Entities.ForEach(
    (ref LocalToWorld gunTransform, ref Rotation gunRotation, ref CharacterGun gun, ref GunInput gunInput) =>{


    gun.Duration += dt;
    if ((gun.Duration > gun.Rate) || (gun.WasFiring == 0))
    {
    if (gun.Bullet != null)
    {
    var e = PostUpdateCommands.Instantiate(gun.Bullet);
    Translation position = new Translation { Value = new float3(gunTransform.Position.x, gunTransform.Position.y, gunTransform.Position.z) };
    position.Value = math.forward(gunRotation.Value) + 6.0f;
    Rotation rotation = new Rotation { Value = gunRotation.Value };
    PhysicsVelocity velocity = new PhysicsVelocity
    {
    Linear = gunTransform.Forward * gun.Strength * Time.DeltaTime,
    Angular = float3.zero
    };
    PostUpdateCommands.SetComponent(e, position);
    PostUpdateCommands.SetComponent(e, rotation);
    PostUpdateCommands.SetComponent(e, velocity);
    }
    gun.Duration = 0;
    }
    gun.WasFiring = 1;
    }
    );
    }
    }


    Thoughts? Thanks