- Home /
CompareBaseObjectsInternal can only be called from the main thread.
Hey. Could someone help me find the error in this script as unity does not tell me the exact line of the error and after searching I still carnt find an answer, please help! Thanks
ERROR : CompareBaseObjectsInternal 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.
CODE : using UnityEngine; using System.Collections; using System.Threading; public class chunkLoader : MonoBehaviour { public Transform generatorObject; Component gen; public int renderDistX = 0; public int renderDistZ = 0; Vector3 pos = new Vector3(); GameObject[,] chunks = new GameObject[100,100]; Vector2[,] renderTest = new Vector2[0,0]; // Use this for initialization void Start () { renderTest = new Vector2[renderDistX,renderDistZ]; } void updateChunk(){ for(int x = 0 ; x < renderTest.GetLength(0) ; x++){ for(int z = 0 ; z < renderTest.GetLength(0) ; z++){ renderTest[x,z] = new Vector2((Mathf.Round(pos.x/8)+x)*8,(Mathf.Round((pos.z/8)+z)*8)); } } for(int x = 0 ; x < renderTest.GetLength(0) ; x++){ for(int z = 0 ; z < renderTest.GetLength(0) ; z++){ if(chunks[(int)(renderTest[x,z].x),(int)(renderTest[x,z].y)]==null){ chunks[(int)(renderTest[x,z].x),(int)(renderTest[x,z].y)] = generatorObject.GetComponent<generator>().createChunk((int)(renderTest[x,z].x),(int)(renderTest[x,z].y)); } } } } // Update is called once per frame void Update () { pos = transform.position; Thread genThread = new Thread(updateChunk); if(!genThread.IsAlive){ genThread.Start(); } } }
Answer by Bunnybomb7670 · May 09, 2014 at 09:31 AM
Seems like you must be calling Unity specific functions from another thread, compare gameobjects means you are checking if one gameobject equals another, apparently this is not threadsafe, so you would need to make sure your separate thread does NOT interract with Unity functions at all. Check this thread : http://forum.unity3d.com/threads/195902-CompareBaseObjectsInternal-can-only-be-called-from-the-main-thread-annoying
Thanks, but then how would it be possible to do what I am trying to do as it lags so i need it in anouther thread? xD
What part is lagging? is it the Voxel $$anonymous$$eshing?