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();
temp.AddComponent();
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.
↧