Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 IgorAherne · Dec 30, 2013 at 10:22 PM · errordebugapithreadinginternal

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
 
Comment
Add comment · Show 3
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 Owen-Reynolds · Dec 30, 2013 at 11:36 PM 1
Share

It looks like the "don't use constructors on subclasses of $$anonymous$$onobehavior" error. Are there any of those involved?

avatar image IgorAherne · Dec 30, 2013 at 11:50 PM 0
Share

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!

avatar image billykater · Dec 31, 2013 at 12:11 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

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 IgorAherne · Dec 31, 2013 at 12:07 AM 0
Share

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!

avatar image Bunny83 · Dec 31, 2013 at 12:28 AM 1
Share

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.

avatar image IgorAherne · Dec 31, 2013 at 12:44 AM 0
Share

Epic Win $$anonymous$$any many many thanks :)

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

21 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

Related Questions

Warning messages are turning into Internal Compiler Errors...? 6 Answers

GooglePlayServices & GoogleMobileAds 0 Answers

Sprite trim from code using alpha. 1 Answer

C# script failing silently? 0 Answers

Error importing asset from google poly api in android build unity 0 Answers


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