- Home /
Count of objects unity dots
I want to find count of object and if zero finish the game.I have monobehaviour for ui control and get count from ballcount entity. when game start finish ui is being activated. (I instantiate objects that counted, in a monobehaviour start function) Here is my code:
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
using Unity.Collections;
using UnityEngine;
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
public class BallCountController : SystemBase
{
EntityQuery _query;
EntityManager manager;
protected override void OnCreate()
{
_query = EntityManager.CreateEntityQuery(typeof(BallCount));
}
protected override void OnUpdate()
{
manager = World.EntityManager;
float deltaTime = Time.DeltaTime;
var entities = _query.ToEntityArray(Allocator.TempJob);
var ballCounts = _query.ToComponentDataArray<BallCount>(Allocator.TempJob);
var ballcount = ballCounts[0];
EntityQuery query = GetEntityQuery(ComponentType.ReadOnly<BallFollowData>());
ballcount.ballCount = query.CalculateEntityCount();
manager.SetComponentData<BallCount>(entities[0],ballcount);
}
}
Answer by andrew-lukasik · Aug 09, 2021 at 01:01 AM
You don't need this system to do that
public class MyGameplayController : MonoBehaviour
{
EntityManager _entityManager;
EntityQuery _queryBalls;
void Start ()
{
_entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
_queryBalls = _entityManager.CreateEntityQuery(
ComponentType.ReadOnly<BallFollowData>()
);
}
void Update ()
{
int numBalls = _queryBalls.CalculateEntityCount();
Debug.Log($"number of balls: {numBalls}");
}
}
But if you really want to:
[UpdateInGroup( typeof(FixedStepSimulationSystemGroup) )]
public class BallCountController : SystemBase
{
EntityQuery _query;
protected override void OnCreate()
{
_query = GetEntityQuery( ComponentType.ReadOnly<BallFollowData>() );
EntityManager.CreateEntity( typeof(BallCount) );
SetSingleton<BallCount>( new BallCount{
ballCount = 0
} );
}
protected override void OnUpdate ()
{
int numBalls = _query.CalculateEntityCount();
SetSingleton<BallCount>( new BallCount{
ballCount = numBalls
} );
}
}
public class MyGameplayController : MonoBehaviour
{
BallCountController _ballCountController;
void Start ()
{
_ballCountController = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<BallCountController>();
}
void Update ()
{
var ballCount = _ballCountController.GetSingleton<BallCount>();
int numBalls = ballCount.ballCount;
Debug.Log($"number of balls: {numBalls}");
}
}
Uh this save me 2 script. Things work when I manually ignore the first frame. Any suggestion for that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
public class UIManager : MonoBehaviour
{
public GameObject fail;
private EntityManager manager;
public Entity obj;
EntityQuery _queryBalls;
bool once = false;
// Start is called before the first frame update
void Start()
{
manager = World.DefaultGameObjectInjectionWorld.EntityManager;
_queryBalls = manager.CreateEntityQuery(
ComponentType.ReadOnly<BallFollowData>()
);
}
// Update is called once per frame
void Update()
{
if(once && _queryBalls.CalculateEntityCount() == 0)
{
fail.SetActive(true);
Time.timeScale = 0;
}
else
{
once = true;
}
}
}
Not sure, but errors that are fixed this way are usual sign of a race condition - situation where order of execution makes or brakes the program.Things work when I manually ignore the first frame
new cool things thanks very much... Only advantage of using singleton is accessing easily ?
SetSingleton<BallCount>( new BallCount{
ballCount = numBalls
} );
}
Well, it's a singleton. It's useful when you need to makes sure there is a single instance of something.
Dude I am back with new question. Do you know about textures?
Your answer

Follow this Question
Related Questions
[ECS] Create Entities from Job ( IJobForEachWithEntity) using an EntityCommandBuffer 1 Answer
Best way to get data from a Monobheaviour in a JobComponentSystem? 2 Answers
[ECS] DefaultWorldInitialization.Initialize take 50MB 1 Answer
Unity (ECS & DOTS) Entity Sinking 0 Answers
How to make singleplayer/multiplayer game with DOTS 0 Answers