Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DrSpritz · Nov 17, 2015 at 10:33 PM · networkingspawn

UNET Cant spawn object with RegisterSpawnHandler

Hello!

I need help with spawning dynamicaly created object in the scene. Below I describe a fragment of code that is used to spawn:

 public void Update()
     {
         if(isServer)
         {
             if(Input.GetKeyDown(KeyCode.Space))
             {
                 GameObject trololo = GameObject.CreatePrimitive (PrimitiveType.Capsule);
                 trololo.AddComponent<NetworkIdentity>();
 
                 NetworkHash128 creatureAssetId = NetworkHash128.Parse("e2656f");
 
                 ClientScene.RegisterSpawnHandler(creatureAssetId, SpawnH, UnspawnH);
 
                 NetworkServer.Spawn(trololo, creatureAssetId);
             }
         }
     }
     
     public GameObject SpawnH(Vector3 position, NetworkHash128 hash)
     {
         Debug.Log ("lkahsgdfyagf98q23g98dq23d67qgfwd796qg29d8o7qtwr78");
 
         GameObject trololo = GameObject.CreatePrimitive (PrimitiveType.Capsule);
         
         return trololo;
     }
     
     public void UnspawnH(GameObject obj)
     {
         obj.SetActive (false);
     }

In result i have error Failed to spawn server object, assetId=0000000000000e2656f netId=2 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

alt text

What do i wrong?

err.jpg (274.4 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
1
Best Answer

Answer by seanr · Nov 17, 2015 at 10:35 PM

you are calling "ClientScene.RegisterSpawnHandler" on the server, not on the client. it must be called on the client.

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 DrSpritz · Nov 17, 2015 at 10:40 PM 0
Share

After I wrote this question, I came up with the same idea. Yes, you're right, everything works fine! Thank you! =)

avatar image MilesAway1980 · Nov 23, 2015 at 10:39 PM 0
Share

Can someone clarify how this works?
First off, if you're dynamically creating objects that can be spawned, why does this have to be done on the client? Why can't the server establish and register new spawnable objects?

Secondly, if a client registers a spawnable object with RegisterSpawnHandler, how goes NetworkServer.Spawn() work? Spawn() requires a GameObject as a variable so that it knows what to spawn. How does it get this GameObject?

The signal to spawning is done within the server side code, so it needs to know what object to spawn. How can the server spawn a copy of the object that's defined by the client?

I must be missing something, but what?

avatar image DrSpritz MilesAway1980 · Nov 24, 2015 at 08:51 AM 0
Share

I will try to write my thoughts about it.

First off, if you're dynamically creating objects that can be spawned, why does this have to be done on the client?

Only on clients side objects is spawn as GameObject, not the server. In "master-client" aka "host" (server + client) case, objects spawn only for the server-side local client and this objects interpretiert as server-side objects and it can be call commands [cmd] and other specifical server methods.

Why can't the server establish and register new spawnable objects?

Because the server do not work with GameObject, only assetId (NetworkHash128), which is associated with registrated prefabs (GameObject) on client side.

Secondly, if a client registers a spawnable object with RegisterSpawnHandler, how goes NetworkServer.Spawn() work? Spawn() requires a GameObject as a variable so that it knows what to spawn. How does it get this GameObject?

NetworkServer.Spawn(GameObject prefab) have overload method NetworkServer.Spawn(GameObject prefab, NetworkHash128 assetId), i do not know, why it does not exist in docs, because i do not understand how may works ClientScene.RegisterSpawnHandler() without this method.

Server send to client spawn message ($$anonymous$$sgType.ObjectSpawn) use NetworkServer.Spawn(GameObject prefab, NetworkHash128 assetId), client receive it and get spawn message, which contain assetId, this assetId associate with registered prefab or spawn handlers on client side and spawn object. If you seen spawnhandler delegate, you should notice that is method return GameObject, which use as a prefab. Look to the source code which implement OnObjectSpawn() method from ClientScene.cs

The signal to spawning is done within the server side code, so it needs to know what object to spawn. How can the server spawn a copy of the object that's defined by the client?

For the ClientScene.RegisterSpawnHandler() I described above. For prefab case is simply. All registered prefabs need to contain NetworkIdentity. This component have assetId property, which use to associate prefab on client side. See the source code OnObjectSpawn().

This is my imagination of how it works and I could be wrong.

Sorry for my english =)

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

44 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 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 avatar image avatar image

Related Questions

How do I rotate a Networked gameobject on spawn? 0 Answers

NetworkServer is not active. Cannot spawn objects without an active server. 0 Answers

UNET 5.4 - Spawn an object from client...and move it around from client....? 1 Answer

NetworkServer.SpawnWithClientAuthority not working? 1 Answer

Is it possible to load prefabs from an assetbundle at runtime and spawn these prefabs in a client server scene? 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