• 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
1
Question by hoffmanuel · Apr 23, 2013 at 08:25 AM · c#error

Unity .net Remoting Server get_animation can only be called from the main thread

I am working on a project where I am having two Unity Projects that need to communicate with each other. I am trying to solve this by using the .net Remoting Framework. For That I created a dll which both Unity projects will use. The dll consists of:

MyRemotableObject.cs

 public class MyRemotableObject : MarshalByRefObject
 {
     public MyRemotableObject()
     {
     
     }
     public void NotifyStatusChange(int status)
     {
         Cache.GetInstance().Status = 0;
     }
     public int GetCreditCount()
     {
         return Cache.GetInstance().CreditCount;
     }
 }

Cache.cs

 public class Cache
 {
     private static Cache myInstance;
     public static IObserver Observer;
     private Cache()
     {

     }
     public static void Attach(IObserver observer)
     {
         Observer = observer;
     }
     public static Cache GetInstance()
     {
         if(myInstance==null)
         {
             myInstance = new Cache();
         }
         return myInstance;
     }
     
     public int Status
     {
         set
         {
             Observer.NotifyFinished(value);
         }
     }
     public int CreditCount
     {
         get
         {
             return Observer.QueryCreditCount();
         }
     }
 }

IObserver.cs

 public interface IObserver
 {
     void NotifyFinished(int status);
     int QueryCreditCount();
 }

Now I have my Menu - Unity project, acting as the remoting server

MenuController.cs

 public class MenuController : MonoBehaviour, IObserver
 {
     private object lockObject;
     List<ControllerBase> controllers;
     private MyRemotableObject remotableObject;
     private System.ComponentModel.Container components = null;
 
     void Awake()
     {
         lockObject = new object();
         try
         {
             remotableObject = new MyRemotableObject();
 
             //für fehler:  //http://www.mycsharp.de/wbb2/thread.php?postid=199935
             //************************************* TCP *************************************//
             // using TCP protocol
             TcpChannel channel = new TcpChannel(124);
             ChannelServices.RegisterChannel(channel, false);
             RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyRemotableObject), "TargetShooterMenu", WellKnownObjectMode.SingleCall);
             //************************************* TCP *************************************//
             RemotableObjects.Cache.Attach(this);
         }
         catch (Exception e)
         {
             Debug.Log(e.ToString());
         }
 
         controllers = new List<ControllerBase>();
         foreach (GameObject controllerObject in GameObject.FindGameObjectsWithTag(GlobalNames.Tags.CONTROLLEROBJECT))
         {
             if (controllerObject.GetComponent<ControllerBase>())
                 controllers.Add(controllerObject.GetComponent<ControllerBase>());
         }
     }
     delegate void PresentNameInputControllerDelegate(int status);
     private void PresentNameInputController(int status)
     {
         if (status == (int)LevelStatusCode.OK)
             foreach (ControllerBase controller in controllers)
             {
                 controller.Hide();
                 if (controller.GetType() == typeof(NameInputController))
                     controller.Show();
             }
     }
     public void NotifyFinished(int status)
     {
         Debug.Log("Notify");
         lock (lockObject)
         {
             PresentNameInputControllerDelegate d = PresentNameInputController;
             
             d(status);
         }
     }
     public int QueryCreditCount()
     {
         Debug.Log("Query");
         return 100;
     }
 }

This Server implements the IObserver Functions NotifyFinished and QueryCreditCount (returns dummy value for the moment) When calling the NotifyFinished function from the client, following error occurs:

get_animation 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. Can someone tell me, how to solve this problem? Thanks in advance, Hoffmanuel
Comment
Add comment
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

1 Reply

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

Answer by hoffmanuel · Apr 24, 2013 at 10:52 AM

After lots of searching, i came to the solution: Using the Loom Unity Package from: Unity Gems entry about threading and using it like mentioned in Unity answers entry about threading:

 void Start()
 {
     var tmp = Loom.Current;
     ...
 }
 //Function called from other Thread
 public void NotifyFinished(int status)
 {
     Debug.Log("Notify");
     try
     {
         if (status == (int)LevelStatusCode.OK)
         {
             Loom.QueueOnMainThread(() =>
             {
                 PresentNameInputController();
             });
         }
     }
     catch (Exception e)
     {
         Debug.LogError(e.ToString());
     }
 }
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

C# Script error (error CS8025: Parsing error) 0 Answers

Distribute terrain in zones 3 Answers

mono in unity 3d and .net remoting 0 Answers

cant kill more than one enemy C# 2 Answers

Show Strings With Last Characters of the first Word 1 Answer


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