Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 cchpackers1 · Jun 14, 2019 at 03:01 PM · errorobjecterror message

Using entities and jobs

Below is my code, and I do not know what to do with the errors

 public class TestFinal : MonoBehaviour
 {
     [SerializeField] private Mesh mesh;
     [SerializeField] private Material material;
     public Texture2D _imageMap;
     [SerializeField] private bool useJobs;
     private NativeArray<Entity> entityArray;
     //public peerAtAudio Audiopeer;
     public int startScale;
     public int startMultiplier;
     public EntityManager entityManager;
     public EntityArchetype entityArchetype;
     public NativeArray<Entity> entityArray1;
     private void Start()
     {
         entityArchetype = entityManager.CreateArchetype(typeof(Translation), typeof(_bandComp), typeof(RenderMesh), typeof(LocalToWorld), typeof(Transform)); //entity that contains these
 
         entityArray1 = new NativeArray<Entity>(_imageMap.width * _imageMap.height, Allocator.Temp);//Allocator.Temp is chossen temp will be temporary to just establish the 2 entities
 
         Color[] pixels = _imageMap.GetPixels(0, 0, _imageMap.width, _imageMap.height);
 
         entityManager = World.Active.EntityManager;
 
 
         entityManager.CreateEntity(entityArchetype, entityArray1); //create entity in entity manager 
         int i = 0;
         for (int x = 0; x < _imageMap.width - 1; x++)
         {
             int iter = x * _imageMap.width;
             for (int y = 0; y < _imageMap.height - 1; y++)
             {
 
                 Color color1 = pixels[(iter) + y];
 
                 Entity entity = entityArray1[i]; //put each entity in the NativeArray 
 
                 entityManager.SetComponentData(entity, new Translation
                 {
                     Value = new float3(x, 0, y)
                 });
                 entityManager.SetComponentData(entity, new _bandComp
                 {
                     _band = UnityEngine.Random.Range(0, 8)
                 });
                 Material mat = new Material(material);
                 mat.color = color1;
                 entityManager.SetSharedComponentData(entity, new RenderMesh
                 {
 
                     mesh = mesh,
                     material = mat
                 });
                 i++;
             }
             i++;
         }
     }
     private void Update()
     {
         int size = _imageMap.width * _imageMap.height;
         ReallyToughParallelJob reallyToughParallelJob = new ReallyToughParallelJob
         { deltaTime = Time.deltaTime, entityMang = entityManager, entityArr = entityArray1 };
         JobHandle jobHandle = reallyToughParallelJob.Schedule(size, 100); // 100 means each job is handeling 100 components 
         jobHandle.Complete();
 
 
 
         /* ReallyToughParallelJob reallyToughParallelJob = new ReallyToughParallelJob { } 
          float startTime = Time.realtimeSinceStartup;
 
          NativeArray<float3> positionArray = new NativeArray<float3>(cubezList.Count, Allocator.TempJob);// in a regular game you will not need to instantiate this every time 
          NativeArray<float> moveYArray = new NativeArray<float>(cubezList.Count, Allocator.TempJob);
          for (int i = 0; i < cubezList.Count; i++)
          {
              positionArray[i] = cubezList[i].transform.position;
              moveYArray[i] = cubezList[i].moveY;
 
          }
          ReallyToughParallelJob reallyToughParallelJob = new ReallyToughParallelJob
          { deltaTime = Time.deltaTime, positionArr = positionArray, moveYArr = moveYArray };
          JobHandle jobHandle = reallyToughParallelJob.Schedule(cubezList.Count, 100); // 100 means each job is handeling 100 components 
          jobHandle.Complete();
          for (int i = 0; i < cubezList.Count; i++)
          {
              cubezList[i].transform.position = positionArray[i];
              cubezList[i].moveY = moveYArray[i];
          }
          positionArray.Dispose();
          moveYArray.Dispose();
          */
     }
 
 
 
     [BurstCompile]
     public struct ReallyToughParallelJob : IJobParallelFor
     {
         public NativeArray<Entity> entityArr;
         public EntityManager entityMang;
         public float deltaTime;
 
         public void Execute(int index)//index for multiple objects
         {
             Entity entity = entityArr[index];
             float3 val = entityMang.GetComponentData<Translation>(entity).Value;
             float3 val2 = new float3(1 * deltaTime, 0, 0);
             val = val + val2;
             entityMang.SetComponentData(entity, new Translation
             {
                 Value = val
             });
 
         }
     }
 }





I get these two errors:


NullReferenceException: Object reference not set to an instance of an object TestFinal.Start () (at Assets/tutorial/TestFinal.cs:34)


InvalidOperationException: ReallyToughParallelJob.entityMang is not a value type. Job structs may not contain any reference types. Unity.Jobs.LowLevel.Unsafe.JobsUtility.CreateJobReflectionData (System.Type type, Unity.Jobs.LowLevel.Unsafe.JobType jobType, System.Object managedJobFunction0, System.Object managedJobFunction1, System.Object managedJobFunction2) (at C:/buildslave/unity/build/Runtime/Jobs/ScriptBindings/Jobs.bindings.cs:96) Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[T].Initialize () (at C:/buildslave/unity/build/Runtime/Jobs/Managed/IJobParallelFor.cs:23) Unity.Jobs.IJobParallelForExtensions.Schedule[T] (T jobData, System.Int32 arrayLength, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn) (at C:/buildslave/unity/build/Runtime/Jobs/Managed/IJobParallelFor.cs:50) TestFinal.Update () (at Assets/tutorial/TestFinal.cs:81)


Comment
Add comment · Show 2
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 Captain_Pineapple · Jun 14, 2019 at 03:40 PM 1
Share

Please reformat that code to make it readable. There is a button for code sections with 1 and 0 on it just above the textfield.

avatar image cchpackers1 · Jun 14, 2019 at 08:32 PM 0
Share

The code should now be readable

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Jun 15, 2019 at 09:37 AM

There are 2 issues in your code.

One is a nullref wich probably results from entityManager.CreateEntity(entityArchetype, entityArray1); where the array actually is not filled with valid entries.

The second and more problematic issue is that you can not use any non value types in a job. A value type is for example float, int, double, Vector3 and so on. There is a special job type that enables using Transforms but that is all there is to it. Your EntityManager is not allowed since it is a "Reference Type". And i guess same goes for Entitys themselves. On the latter i'm not sure.

Comment
Add comment · Show 3 · 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 cchpackers1 · Jun 15, 2019 at 05:40 PM 0
Share

Would I just fill the native entity array like any other array? and what is the job called that uses transforms

avatar image Captain_Pineapple cchpackers1 · Jun 16, 2019 at 04:59 PM 0
Share

Hey,

There is the IJobParallelForTransform Interface. It somehow grants access to a given Transform when calling the Execution method.


assu$$anonymous$$g that the native array question of you points to the nullreference issue: To be really sure on your actual issue you should rework your code formatting once more. The Line 34 that is given by the error message is empty in the code you provided so i can only guess where your code failed. To debug this you could include some checks after filling the arrays. Iterate over it and check for "null"-entrys.

avatar image cchpackers1 Captain_Pineapple · Jun 17, 2019 at 01:52 PM 0
Share

Thanks I will try some of these methods!

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

145 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 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

Unity3D 4.2.0 Crasing ... 0 Answers

Object Reference not set to an instance of an Object Help 1 Answer

Error, I have no idea why :( 2 Answers

Was gameObject.GetChild(childsName); a thing? 2 Answers

Xcode error when Archiving linker command failed with exit code 1 (use -v to see invocation) clang 1 Answer


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