- Home /
threading if() problem
Hey!
Does anybody know what this error means?
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.
What I try to do is run a function inside a thread. In this function I compare a variable inside one class to a variable of another class. Values are initialized by constructors before the function is even called.
I don't seem to be using any of unity's API. no calls similar to gameObject.name or getComponent() are there.
NODE COUNTER EDITOR
//NodeCounterEditor
//folowing function gets called from the OnInspectorGUI, get's fed a class LookFurtherArgs with 3 starting parameters
void LookFurther(object _work){
LookFurtherargs _recieved = (LookFurtherargs)_work;
GameObject currentNode = _recieved.current;
GameObject cameFrom = _recieved.camefrom;
NodeSpark _NScurrentNode = (NodeSpark)_recieved.carriedScript;
if(remoteAdv > 0){
//do something
for(int x = 0; x < _NScurrentNode.neighbours.Count; x++){
//do something else
if(!BanCheck(check1) && check1 != cameFrom ){ //PROBLEMATIC AREA, BanCheck is a function returns true/false, not the cause of problem though.
remoteAdv--;
object _inPass = new LookFurtherargs(_NScurrentNode.neighbours[x],currentNode,_NScurrentNode._NSneighbour[x]);
LookFurther(_inPass); //continue recursion with the neighbor that is available to explore
remoteAdv++;
}
}//end of if
}//end of function
LookFurtherArgs. used as a feeding object for a function LookFurther, that has to take 3 parameters, but thread only allows object, so we are using this class to shove it in that thread
using UnityEngine;
using System.Collections;
public class LookFurtherargs {
public GameObject current;
public GameObject camefrom;
public NodeSpark carriedScript;
public LookFurtherargs(GameObject x, GameObject c, NodeSpark j){
current = x;
camefrom = c;
carriedScript = j;
}//end of constructor
public NodeSpark component(GameObject a){
return a.GetComponent<NodeSpark>();
}
}
finally, NodeSpark
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NodeSpark : MonoBehaviour {
public List<GameObject> neighbours = new List<GameObject>();
public List<string> accessN;
public List<BranchShell> pathHolder = new List<BranchShell>();
public List<NodeSpark> _NSneighbour = new List<NodeSpark>();
public string carriedName; //simmilar to the one below
public GameObject GO; //get's a value from NodeSparkEditor, during OnEnable(), through target as GameObject
}//end of class
It looks like the "don't use constructors on subclasses of $$anonymous$$onobehavior" error. Are there any of those involved?
I am calling a function via paramererizedThreadStart, with a parameter of type ClassA, which is fed in as object. (Class A has several variables list that are instantiated without Awake() or start(), since I need them to be ready when working in Editor)
In that function I then cast the object down to Class A and assign it to the value of type Class A. All of that happens in the function. Now, I check if a values of those lists equal to the other variables or not.
I will update the question in a second with the code, thank you for helping!
As I already demonstrated, comparing two gameobjects in another threads is not possible,because it will internally invoke a custom comparer implemented by unity. That is also why you get "CompareBaseObjectsInternal" as the offending function because it actually uses the unity api while comparing a gameobject to something.
Answer by billykater · Dec 30, 2013 at 11:45 PM
The most probable cause here is that you are comparing two objects derived from UnityEngine.Object in this if. Unity doesn't allow any api calls in different threads than the main one, which also includes the custom comparer unity uses on UnityEngine.Object derived things. If you do something like
GameObject a;
GameObject b;
...
if(a != b)
in another thread it will throw the given error.
Thank you, so comparing GameObjects is not allowed as well? grrrr. Well, I will compare them by custom indexes then, let me try to see if that's the solution
Thanks!
System.Object.ReferenceEquals should always work with reference types. $$anonymous$$eep in $$anonymous$$d that Unity's "null-trick" won't work in this case. So references to destroyed objects will still be valid and will fail a comparison with null. However the object can't be used anymore.
Once all references to such a ghost object are gone it will be garbage collected.