Can i use UnityObjectRef in job?

in job when i call UnityObjectRef.Value it show error

UnityException: InstanceIDToObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Resources.InstanceIDToObject (System.Int32 instanceID) (at :0)
Unity.Entities.UnityObjectRef1[T].op_Implicit (Unity.Entities.UnityObjectRef1[T] unityObjectRef) (at ./Library/PackageCache/com.unity.entities@1.3.0-pre.4/Unity.Entities/Serialization/UnityObjectRef.cs:118)

when i open UnityObjectRef.cs:118. i see that it is calling Resources.InstanceIDToObject

in someway, can i use UnityObjectRef in job?

or can i pass List<Transform> into job? because i only need to read this list in job.
Capture
i can convert List<Transform> to NativeList. but i have to loop all element in list.

If you’re intending to access data on the transforms in the job, that’s a no-go since those are main thread only. You should instead be using TransformAccessArray and IJobParallelForTransform.

1 Like

thanks your reply. but when i use it in IJobEntity and call its property. it show same error.
Capture

Capture

UnityException: GetTransform can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Jobs.TransformAccessArray.GetTransform (System.IntPtr transformArrayIntPtr, System.Int32 index) (at :0)
UnityEngine.Jobs.TransformAccessArray.get_Item (System.Int32 index) (at :0)

i have to use it in IJobParallelForTransform?

Yeah. Accessing transforms directly isn’t allowed anywhere but the main thread, much like most of Unity’s non-DOTS API. You need to use the job type I mentioned which provides a TransformAccess struct per iteration corresponding to one transform.

2 Likes