- Home /
Question by
pastersteli · Aug 04, 2021 at 04:41 PM ·
physicsdestroycollidedots
Entity System Collider And Destroy
Whats wrong with this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
public class CollideAndDestroyController : JobComponentSystem
{
private BuildPhysicsWorld buildPhysicsWorld;
private StepPhysicsWorld stepPhysicsWorld;
private struct DestroyJob : ITriggerEventsJob
{
public ComponentDataFromEntity<CollideAndDestroy> collideAndDestroyGroup;
public ComponentDataFromEntity<BallFollowData> ballFollowGroup;
private EntityCommandBuffer.ParallelWriter commandBuffer;
public void Execute(TriggerEvent triggerEvent)
{
if (collideAndDestroyGroup.HasComponent(triggerEvent.EntityA))
{
if (ballFollowGroup.HasComponent(triggerEvent.EntityB))
{
commandBuffer.DestroyEntity(triggerEvent.EntityB.Index,triggerEvent.EntityB);
}
}
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var destroyJob = new DestroyJob
{
collideAndDestroyGroup = GetComponentDataFromEntity<CollideAndDestroy>(),
ballFollowGroup = GetComponentDataFromEntity<BallFollowData>()
};
return destroyJob.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, inputDeps);
}
}
error: NullReferenceException: Object reference not set to an instance of an object CollideAndDestroyController.OnUpdate (Unity.Jobs.JobHandle inputDeps) (at Assets/Scripts/Systems/CollideAndDestroyController.cs:44)
Comment
Answer by andrew-lukasik · Aug 04, 2021 at 07:28 PM
com.unity.physics 0.50.0-preview.43
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using BurstCompile = Unity.Burst.BurstCompileAttribute;
[UpdateInGroup( typeof(FixedStepSimulationSystemGroup) )]
[UpdateAfter( typeof(StepPhysicsWorld) )]
public partial class CollideAndDestroyController : SystemBase
{
StepPhysicsWorld _stepPhysicsWorld;
EntityCommandBufferSystem _commandBufferSystem;
protected override void OnCreate ()
{
base.OnCreate();
_stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
_commandBufferSystem = World.GetOrCreateSystem<EndFixedStepSimulationEntityCommandBufferSystem>();
}
protected override void OnStartRunning ()
{
base.OnStartRunning();
this.RegisterPhysicsRuntimeSystemReadOnly();
}
protected override void OnUpdate ()
{
var collideAndDestroyData = GetComponentDataFromEntity<CollideAndDestroy>( isReadOnly:true );
var ballFollowDataData = GetComponentDataFromEntity<BallFollowData>( isReadOnly:true );
var destroyJob = new CollideAndDestroyJob{
collideAndDestroyGroup = collideAndDestroyData ,
ballFollowGroup = ballFollowDataData ,
commandBuffer = _commandBufferSystem.CreateCommandBuffer()
};
this.Dependency = destroyJob.Schedule( _stepPhysicsWorld.Simulation , this.Dependency );
var triggerJob = new TriggerAndDestroyJob{
collideAndDestroyGroup = collideAndDestroyData ,
ballFollowGroup = ballFollowDataData ,
commandBuffer = _commandBufferSystem.CreateCommandBuffer()
};
this.Dependency = triggerJob.Schedule( _stepPhysicsWorld.Simulation , this.Dependency );
_commandBufferSystem.AddJobHandleForProducer( this.Dependency );
}
}
[BurstCompile]
struct CollideAndDestroyJob : ICollisionEventsJob
{
[ReadOnly] public ComponentDataFromEntity<CollideAndDestroy> collideAndDestroyGroup;
[ReadOnly] public ComponentDataFromEntity<BallFollowData> ballFollowGroup;
public EntityCommandBuffer commandBuffer;
public void Execute ( CollisionEvent evt )
{
Entity A = evt.EntityA, B = evt.EntityB;
if( collideAndDestroyGroup.HasComponent(A) )
if( ballFollowGroup.HasComponent(B) )
commandBuffer.DestroyEntity(B);
}
}
[BurstCompile]
struct TriggerAndDestroyJob : ITriggerEventsJob
{
[ReadOnly] public ComponentDataFromEntity<CollideAndDestroy> collideAndDestroyGroup;
[ReadOnly] public ComponentDataFromEntity<BallFollowData> ballFollowGroup;
public EntityCommandBuffer commandBuffer;
public void Execute ( TriggerEvent evt )
{
Entity a = evt.EntityA, b = evt.EntityB;
if( collideAndDestroyGroup.HasComponent(a) )
if( ballFollowGroup.HasComponent(b) )
commandBuffer.DestroyEntity(b);
}
}
screenshot-2021-08-06-171529.jpg
(39.4 kB)
No error but dont destroy objects I make collide raise collision events for them. colliding but no events work
Uh I am missing the physics body for one of objects. Thanks now works properly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613041215im_/https://answers.unity.com/themes/thub/images/avi.jpg)