Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pastersteli · Aug 09, 2021 at 12:31 AM · dots

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);
         
     }
 
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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}");
     }
 }
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pastersteli · Aug 09, 2021 at 01:19 AM 0
Share

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;
         }
     }
 }
 
avatar image andrew-lukasik pastersteli · Aug 09, 2021 at 01:41 AM 0
Share

Things work when I manually ignore the first frame

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.
avatar image pastersteli · Aug 09, 2021 at 01:32 AM 0
Share

new cool things thanks very much... Only advantage of using singleton is accessing easily ?

   SetSingleton<BallCount>( new BallCount{
                  ballCount = numBalls
              } );
          }

avatar image andrew-lukasik pastersteli · Aug 09, 2021 at 01:37 AM 0
Share

Well, it's a singleton. It's useful when you need to makes sure there is a single instance of something.

avatar image pastersteli andrew-lukasik · Sep 16, 2021 at 03:54 PM 0
Share

Dude I am back with new question. Do you know about textures?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges