- Home /
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)
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.
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.
Would I just fill the native entity array like any other array? and what is the job called that uses transforms
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.
Thanks I will try some of these methods!
Your answer
Follow this Question
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