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 /
avatar image
1
Question by MilesAway1980 · Nov 25, 2015 at 08:46 PM · unity 5networkingspawningdynamically

Networking: How to spawn dynamically created objects

Hi all, Starting my first foray into networking. Been programming a long time, but would like to create a multiplayer game.

So far, I understand the concept of creating prefabs and registering them with the spawn handler. That seems pretty straight forward. However, the game I want to create has a lot of custom objects made on the fly, and I really don't want to have to create a prefab of each and every one. It would be tedious and a waste of time. So, I'm trying to figure out how to spawn an object created dynamically over the network.

I've been playing with code for a few days and am very frustrated with the dead ends and lack of direction.

The current project has an empty GameObject player registered as the spawnable player within the NetworkManager. Attached to each player object is a script: "Player.cs".

Here's the code I'm playing with. I simply want to create a cube on the spot, register it, attach it to the player, and spawn it on both the host and the client. It seems like it should be really straight forward but I am simply at my wits end.

Player.cs:

 using UnityEngine;
 using UnityEngine.Networking;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Player : NetworkBehaviour {
 
     GameObject myCube;
     NetworkHash128 spawnNum;
 
     GameObject playerCube() {
         GameObject temp = GameObject.CreatePrimitive(PrimitiveType.Cube);
 
         temp.AddComponent<NetworkIdentity>();
         temp.AddComponent<NetworkTransform>();
 
         temp.transform.position = new Vector3(
                 Random.Range (-4, 4), 
                 Random.Range (-4, 4), 
                 Random.Range (-4, 4)
             );
         temp.transform.localScale = new Vector3(
                 Random.Range (1, 3), 
                 Random.Range (1, 3), 
                 Random.Range (1, 3)
             );
         return temp;
     }
 
     GameObject onSpawn(Vector3 position, NetworkHash128 hash) {
         return playerCube ();
     }
 
     void onDespawn(GameObject obj) {
         Destroy (obj);
     }
 
     void Start () {
         spawnNum = NetworkHash128.Parse ("abcde" + Random.Range (0, 1000000));
         ClientScene.RegisterSpawnHandler (spawnNum, onSpawn, onDespawn);
         Init ();
     }
 
     [Server]
     void Init() {
         myCube = playerCube ();
         myCube.transform.parent = transform;
         NetworkServer.Spawn (myCube, spawnNum);
     }
 }

This will spawn two objects on the host, but the client gets the error of:

Failed to spawn server object, assetId=0000000000abcde32127 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

I know I'm doing something wrong, but am clueless as to what it is. I've read dozens of posts, but most of them are specific situations that aren't my case. Any help or guidance? Even just a reposting of this code in a form that actually works correctly would greatly help me. I simply need to see the right way to do it because I don't know what it is.

Thanks.

Comment
Add comment · Show 2
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 stupidoldpoo · Apr 06, 2016 at 05:12 AM 0
Share

Was a solution ever found? I have the same problem

avatar image nwrobel76 · Nov 20, 2016 at 09:02 PM 0
Share

I would be interested in a solution too.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Chirokidz · Feb 05, 2017 at 05:39 AM

In order to avoid sending large amounts of data across the network, network spawning works by never actually sending networked objects across the network. This is the reason for having ClientScene.RegisterPrefab and ClientScene.RegisterSpawnHandler. What these functions do is associate an existing object or a function that creates the correct object with a 128 bit asset ID. When an attempt is made to spawn an object, only the asset id (and relevant information like the position) is sent.

There are a couple of crucial components. First, neither Register function sends anything across the network. In order for dynamic spawning to work, you have to call register on both the client and the server, with the same NetworkHash128.

So in your example, you do this:

 spawnNum = NetworkHash128.Parse ("abcde" + Random.Range (0, 1000000));

since this happens in Start, this does happen on both the client and the server. However, the client and the server both generate a different random Network Hash to register the spawn handler functions under.

The general use case for this would be loading from external configuration rather than prefabs. In that case, you would load the configuration on both the client and server and then spawn using the server object.

If you want to spawn random generated objects on both client and server which cannot be loaded from elsewhere, you will need to serialize the relevant information about the spawn yourself, and then you can have the server make a ClientRPC with the serialized object. The client would then deserialize and register the appropriate spawn handler, which would allow you to spawn it. Note that this would still be an issue with new clients joining midgame, as they would not have the appropriate spawn handlers if they had not received the earlier ClientRPCs.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UNET 5.2: How do you use NetworkServer.SpawnWithClientAuthority()? 1 Answer

Question about Unet and spawning 0 Answers

Change player object - UNET 1 Answer

New Unity 5.1 Networking :- Spawning is not synchronized!!!! 2 Answers

Why does my server show different position for my client? 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