Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

InstantiateSingle can only be called from the main thread

Discussion in 'Scripting' started by julienrobert, Sep 15, 2014.

  1. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    I'm beginner in scripting in Unity 3d.

    I'm trying to call a function inside a C-sharp script from a Javascript. The javascript is intended to manage the incoming OSC messages.

    As shown in the attached example, it works from pushing space button because it is in the main thread but it does not work when it's called from the function dealing with the oscmessages (when sending "/1 bla" in o/c to unity).

    Already tried to workaround it by calling a flag inside the update function of the C-sharp script. It works but it's not going to work for the developed project...

    Any idea how to do it?
     

    Attached Files:

  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    You can't. Unity is mono-threaded and won't let you access any internal method or object from another thread. Flagging it for execution on the next update loop is the only way.
     
  3. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    ok. Then I think my best solution would be to do everything in the same script. So, I need to convert the javascript (not coded myself) into C-sharp. I did most of the work but there is still some lines I could not translate correctly... Could someone have a look to it?...
    Error CS0266: Cannot implicitly convert type `object' to `string'. An explicit conversion exists (are you missing a cast?) (CS0266) (Assembly-CSharp)
     

    Attached Files:

  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I have no idea where "msgValue" is declared.
     
  5. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    The thing is that I don't what type to declare since the osc message value could be int, float, string... when I try to declare string, it says: "Cannot implicitly convert type 'object' to 'string'." and "Cannot implicitly convert type 'object' to 'float'." when I try declaring a float...
    This seams not to be a problem in javascript...
    what should I do?
    Thanks
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    You're writing it in C#, not JavaScript.
    I think you will need to look up what is a Type and how to do casting.
     
  7. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    Ok thanks. Managed to convert the script.
    I must be missing something because I'm still getting the error even if I put the function inside the same script...? What is a main thread?
     
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    The main thread is Unity's update thread, from which all Update methods are called. You should probably first look into what a thread it...
     
  9. julienrobert

    julienrobert

    Joined:
    Sep 15, 2014
    Posts:
    65
    One thing that does not make sense for me...
    Why I don't get any error with the script jitCustomEvents.cs when I call the function balle1 (from the method run) and I get the error with the script OSCReceiver.cs when I call the function balle1 (from the method AllMessageHandler called from another script which is on another thread?) ? In both cases, I call the function from outside the loop update.
     

    Attached Files: