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 Fluffy_Kaeloky · Jun 28, 2015 at 06:43 PM · c#unity 5errornetworkspawn

Unity 5 UNet Spawn as Child.

Hello everyone !

I got a problem trying to instantiate a GameObject across the network as a child on the "Canvas" GameObject, trying to make a UI like what is shown in the Unity 5 UNet presentation (See below.).

Also , instead of LocalPlayer authority, almost everything is handled by the server.

In the video, they create an UI with the Legacy system, I'm trying to create one from a Prefab with the new UI system, hence why I need to spawn the UI as child of the "Canvas" GameObject.

From what I already got, Unity doesn't do it natively.

I already read this answer (http://answers.unity3d.com/questions/989015/unet-spawn-object-to-parent.html) and implemented it, but the ID never gets reconized.

I did called NetworkServer.RegisterHandler(...) by creating an AdvancedNetworkManager that heritate from NetworkManager, and overrided the void OnClientConnect(NetworkConnection conn) function.

Here's some screens and code :

AdvancedNetworkManager.cs :

 public class AdvancedNetworkManager : NetworkManager
 {
     public override void OnClientConnect(NetworkConnection conn)
     {
         base.OnClientConnect(conn);
 
         Debug.Log("Registering Server callbacks ...");
         Debug.Log("Registered Message (ID : " + Message.SetParent + ")");
         NetworkServer.RegisterHandler(Message.SetParent, SetParentMessage.OnSetParent);
     }
 }

Utils.cs :

 public class Utils
 {
     public static void LinkToParent(GameObject child, GameObject parent, bool stay)
     {
         child.transform.SetParent(parent.transform, false);
         Debug.Log("Link to parent called (ID : " + Message.SetParent + ")");
         NetworkServer.SendToAll(Message.SetParent, new SetParentMessage(child, parent, stay));
     }
 }

Message.cs :

 public class Message
 {
     // used for auto-increment
     private enum Type
     {
         SetParent = MsgType.Highest + 1
     }
 
     public const short SetParent = (short)Type.SetParent;
 }
 
 public class SetParentMessage : MessageBase
 {
     public NetworkInstanceId netId;
     public NetworkInstanceId parentNetId;
     public bool woldCoordStay;
 
     public SetParentMessage()
     {
     }
 
     public SetParentMessage(GameObject gameObject, GameObject parent, bool stay)
     {
         netId = GetNetId(gameObject);
         parentNetId = GetNetId(parent);
         woldCoordStay = stay;
     }
 
     private static NetworkInstanceId GetNetId(GameObject obj)
     {
         // NOTE: no error handling
         return obj.GetComponent<NetworkIdentity>().netId;
     }
 
     public static void OnSetParent(NetworkMessage netMsg)
     {
         Debug.Log("OnSetParentCalled (Finally ...)");
         SetParentMessage msg = netMsg.ReadMessage<SetParentMessage>();
         ClientScene.objects[msg.netId].transform.SetParent(ClientScene.objects[msg.parentNetId].transform, msg.woldCoordStay);
     }
 }


The code Intatianting the UI is only called on the server and is as followed :

 uiInstance = Instantiate<GameObject>(combatUI);
     
 NetworkServer.Spawn(uiInstance);
     
 Utils.LinkToParent(uiInstance, canvas, false);


Here's the Log I'm getting on the Client : alt text

And here's the Log I'm getting on the Server(Which is also a Client) : alt text

I really don't get it, is there something wrong in this ?

I hope I'm clear enough.

Thanks for your time. This is getting me crazy !

(UNet presentation : https://www.youtube.com/watch?v=ywbdVTRe-aA)

logserver.png (12.1 kB)
logclient.png (13.0 kB)
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
0
Best Answer

Answer by Fluffy_Kaeloky · Jun 29, 2015 at 06:30 PM

Oh well, I found that replacing

 NetworkServer.RegisterHandler(Message.SetParent, SetParentMessage.OnSetParent);
 

by

 client.RegisterHandler(Message.SetParent, SetParentMessage.OnSetParent);

Gets rid of the unknown ID error, however, it sill gave me a ton of errors.

I just re-implemented my GUI so that every player render every other players UI, now that I think of it , I should have done that immediately.

Oh well ...

Here's a link that helped me : http://gamedev.stackexchange.com/questions/102526/why-will-my-server-not-execute-a-command-sent-by-the-client-in-unity-5-1

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I know not how to implement the "open IAB" in unity 0 Answers

Photon wont sync for the masterclient. 1 Answer

How to Spawn after checking if the clones are destroyed. 1 Answer

Generate gameObject horde, more positioned towards front of horde 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