Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question I was trying to instantiate a cube in the scene using ECS, but the cube is not visible in the scene.

Discussion in 'Entity Component System' started by wuyiji199632, Mar 30, 2023.

  1. wuyiji199632

    wuyiji199632

    Joined:
    Dec 1, 2021
    Posts:
    30
    Here is my source code and the runtime scenarions that deal with the instantiation:


    6a79c434c468e352a5093247cebc6d6.jpg

    32db2a10f1cadd3e55d94adf97207ec.jpg

    b51c664de9b020fb416f60a1287df9b.jpg

    Can anyone tell me why the cube is not instantiated in the scene after being converted into an entity?
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    202
    - You should not be using an outdated version of Entities if you can help it. The latest version is 1.0.0-pre.65 and requires the current version of Unity 2022.2. Old versions of Entities that work on 2021.x are all experimental, will not receive updates or bugfixes, and probably won’t be very useful in the long run.
    - You must post code in code tags, not as screenshots. Screenshots of code are pointless. Give us text we can manipulate and easily try to fix if necessary.
    - Read the informational log at the bottom of your screenshots. SRP are not enabled in your project, so Hybrid Renderer cannot be used. Hybrid Renderer (now named Entities Graphics in the current set of DOTS package versions) can only work with Universal Render Pipeline and High Definition Render Pipeline. One of these must be installed to have default support for rendering anything that was converted from a GameObject to an Entity without anything like a companion GameObject (support for which was, to my recollection, removed anyway).
     
  3. wuyiji199632

    wuyiji199632

    Joined:
    Dec 1, 2021
    Posts:
    30
    I have got what you mean, thanks mate
     
  4. wuyiji199632

    wuyiji199632

    Joined:
    Dec 1, 2021
    Posts:
    30
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Entities;
    5. using Unity.Transforms;
    6. using Unity.Rendering;
    7.  
    8. public class ECSPrefabCreator : MonoBehaviour
    9. {
    10.     public GameObject cube;
    11.     public Material cubeMaterial;
    12.     public Mesh cubeMesh;
    13.  
    14.     private void Start()
    15.     {
    16.         GameObjectConversionSettings conversionSettings= GameObjectConversionSettings.FromWorld(World.DefaultGameObjectInjectionWorld, null);
    17.  
    18.         Entity tempEntityPrefab = GameObjectConversionUtility.ConvertGameObjectHierarchy(cube, conversionSettings);
    19.  
    20.         EntityManager tempEntityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    21.  
    22.         Entity tempCube=tempEntityManager.Instantiate(tempEntityPrefab);
    23.  
    24.         // Set the position of the instantiated cube entity
    25.         tempEntityManager.SetComponentData(tempCube, new Translation { Value = new Unity.Mathematics.float3(0, 1, 0) });
    26.  
    27.  
    28.         // Add the RenderMesh component to the instantiated cube entity      
    29.         tempEntityManager.AddSharedComponentData(tempCube, new RenderMesh
    30.         {
    31.             mesh = cubeMesh,
    32.             material = cubeMaterial
    33.         });
    34.  
    35.     }
    36. }
     
  5. wuyiji199632

    wuyiji199632

    Joined:
    Dec 1, 2021
    Posts:
    30
    I just posted the code in the form of Code Tag, my intention is to visualise the cube instantiated in the scene as I was following a video tutorial on the mechanism of how the ECS runs but somehow got stuck in the step wherein the cube is coverted into an entity and should be instantiated visually in the scene.