- Home /
Client/Server Network Instantiate Problems/Client Can't Connect
First time posting on here, because I decided to see if I could find answers to my problems on here but everybody's code looked similar to mine and worked.
Anyways, I have a button that says "start Server", which will cause the server to be made and create a prefab (which is FPC). Only problem is it can't instantiate the prefab due to an error saying:
UnityEngine.Network:Instantiate(Object, Vector3, Quaternion, Int32) SpawnScript:Spawnplayer() (at Assets/MyAssets/SCripts/SpawnScript.cs:22) SpawnScript:OnServerInitialized() (at Assets/MyAssets/SCripts/SpawnScript.cs:12) UnityEngine.Network:InitializeServer(Int32, Int32, Boolean) Connect:OnGUI() (at Assets/MyAssets/SCripts/Connect.cs:30) My code looks like this:Network instantiated object, Main Camera, has a missing script component attached
Transform playerPrefab = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
I'm sure it is wrong, because I tried to C# a javascript example, and I have not really touched a programming language since 07 in high school (C++)!
For my second problem, I can't get the client to connect to the server created in the first step above. The button is triggered but I can't seem to connect to the server and spawn in.
My "Client" button code looks like this:
if(GUILayout.Button("Connect As Client"))
{
Network.useNat = false;
Network.Connect (connectToIP,connectPort);
}
my "SpawnPlayer" code looks like this:
void Spawnplayer()
{
Transform playerPrefab = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
}
Any help would be nice, as I tried to figure out this problem by myself and I was up till 3 am this morning trying to convert java into C#, which ultimately lead to erasing the script and starting over.
Does the playerPrefab
have a networkView
attached?
Yes, it does. I just checked it, that was the one thing I knew it needed from the start after reading tutorials!
Why are you setting the reference to a player to the instance instantiated by the network?
I was following this code (which is in javascript)
public var playerPrefab : Transform;
function OnServerInitialized(){
Spawnplayer();
}
function OnConnectedToServer(){
Spawnplayer();
}
function Spawnplayer(){
var myNewTrans : Transform = Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
}
So how did you come to the conclusion that replacing myNewTans
with playerPrefab
, which already exists, is a good idea?