- Home /
How do I Network.instantiate an object based on Playerprefs?
Hi, I have posted this before but I may not have explained enough or properly.
Start to end.
I have a game, where you drive around a track. There is 5 cars and 2 tracks. On the car selection screen there are 5 icons, each one representing a car.
If the user taps the 2nd icon, hes selection is save as an int in a PlayerPref. ie Car 1 has an int value of 1 and is saved as PlayePref. (SetInt) Car 2 has an int value of 2 Car 3 has an int value of 3 and so on.
On the next screen, the user can select a track.
Once play is pressed, it loads the selected track and fetches your saved car.
If the user selected car 3, car 1,2,4 and 5 are SetActiveRecursively(false);
So, a working car selection and track part..
Here is where my problem comes in. When I create a server, I have it so It loads a my track, then registers a server. At this point the car I had last, is spawned automatically. So I only needed to select a track when creating a server.
Okay, at this point I'm on a server I created, and it has loaded my car. I am technically Online waiting for people to connect.
This is all on my Phone. Now I need to connect to my own server. ( So I open up Unity)
I refresh my serverlist and hey presto, the server I am currently running on my phone pops up in Unity. Good sofar..
When I click connect I get this message
But the scene wont load that the server is running. I assume its because i have no NetworkView attached? So I attach a NetworkView to my car prefab. (says my current car is observed, and the view ID is 1)
Okay..Rebuild and compile to APK..now to test again. I run the game on my phone, and start a server, this time my car has a networkView attatched.
Now I have to connect in Unity again.. So i connect and this time a different error.
PS: The track and Server screen are 2 different scenes. It stays on the server selection screen when I connect to the server.
Can someone please help me get this to work?
Here is my script that handles everything on the server side. Keep in mind everything works as expected, besides the View ID.
I assumed I wouldn't need a Prefab to spawn, as it is there waiting for the player already. So there is no Network.Instantiate for a prefab.
Help would be greatly appreciated! Cheers, Tim.
var serverStart : GUITexture;
var refreshServerlist : GUITexture;
var meiko : GUITexture;
var cON : Color;
var cOFF : Color;
var cOFF2 : Color;
var customskin : GUISkin;
var underpass : GUITexture;
var createtheserver : GUITexture;
var SavedPlayer : int = 0;
var selectedTrack : int = 0;
var native_width : float = 1280;
var native_height : float = 720;
var countdown : float = 0;
var gameNameRoom : String = "";
var gameComment : String = " here";
var startserveranim : GameObject;
var startserveranim2 : GameObject;
var startrefreshanim : GameObject;
var noserveranim : GameObject;
var createserver : GameObject;
var server_ISReady : boolean;
var registered : boolean;
var refreshing : boolean ;
var creatingserver : boolean ;
var r1 : float = 0;
var r2 : float = 0;
var r3 : float = 0;
var r4 : float = 0;
var r11 : float = 0;
var r21 : float = 0;
var r31 : float = 0;
var r41 : float = 0;
var Skinserverlist : GUISkin;
var playername : String = "";
private var playerCount: int = 0;
private var hostData : HostData [];
private var gameName : String = "Test Server";
function Awake(){
playername = PlayerPrefs.GetString ("playername");
SavedPlayer = PlayerPrefs.GetInt ("SelectedCar");
}
function StartServer (){
Network.InitializeServer (10, 25001, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, gameNameRoom , gameComment);
}
function OnConnectedToServer () {
Debug.Log("Player " + playerCount++ +
" connected from " + playername);
Debug.Log("Connected to server");
}
function RefreshHostList (){
MasterServer.ClearHostList();
MasterServer.RequestHostList(gameName);
refreshing = true;
MasterServer.PollHostList();
refreshServerlist.color = cOFF;
}
function OnServerInitialized () {
Debug.Log ("Server Initialized");
}
function OnMasterServerEvent (mse:MasterServerEvent){
if (mse == MasterServerEvent.RegistrationSucceeded){
LoadSelectedTrack ();
registered = true;
Debug.Log ("Registred Server");
}
}
function Update(){
if(server_ISReady){
createtheserver.color = cON;
}
if(!server_ISReady){
createtheserver.color = cOFF2;
}
if(Input.GetKeyDown(KeyCode.Escape) == true){
if (creatingserver)
Application.LoadLevel (10);
else
Application.LoadLevel (0);
}
countdown -= Time.deltaTime;
if (countdown <0){
countdown = 0;
refreshServerlist.color = cON;
}
if(MasterServer.PollHostList().Length > 0){
refreshing = false;
Debug.Log (MasterServer.PollHostList().Length);
hostData = MasterServer.PollHostList();
}
for (var touch : Touch in Input.touches){
if (touch.phase == TouchPhase.Ended && serverStart.HitTest (touch.position)) {
startserveranim.animation.Play();
startserveranim2.animation.Play();
StartgetServerready ();
}
if (touch.phase == TouchPhase.Ended && createtheserver.HitTest (touch.position)) {
if(server_ISReady)
StartServer ();
else
Debug.Log ("not ready");
}
if (touch.phase == TouchPhase.Ended && refreshServerlist.HitTest (touch.position)) {
countdown = 7;
startrefreshanim.animation.Play();
noserversnotice ();
RefreshHostList ();
}
if (touch.phase == TouchPhase.Ended && meiko.HitTest (touch.position)) {
selectedTrack = 1;
server_ISReady = true;
meiko.color = cON;
underpass.color = cOFF;
}
if (touch.phase == TouchPhase.Ended && underpass.HitTest (touch.position)) {
selectedTrack = 2;
server_ISReady = true;
meiko.color = cOFF;
underpass.color = cON;
}
}
}
function OnGUI (){
var rx : float = Screen.width / native_width;
var ry : float = Screen.height / native_height;
GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (rx, ry, 1));
GUI.skin = Skinserverlist;
if (!Network.isServer && !Network.isClient){
if(hostData){
for (var i:int = 0; i<hostData.length; i++){
if( GUI.Button(Rect(110, 155, 300, 100),hostData [i].gameName)){
Network.Connect(hostData[i]);
}
}
}
GUI.skin = customskin;
GUI.Button (Rect (r11, r21, r31, r41),playername);
GUI.Button (Rect (r1, r2, r3, r4),"Welcome to the lobby,");
}
}
function noserversnotice (){
yield WaitForSeconds (6);
if( MasterServer.PollHostList().Length == 0){
noserveranim.animation.Play();
}
}
function StartgetServerready (){
creatingserver = true;
createserver.animation.Play();
}
function LoadAfterDelay(levelName: String){
Application.LoadLevel(levelName);
}
function LoadSelectedTrack (){
if (selectedTrack == 1 )
LoadAfterDelay("MeikoMotorPark");
if (selectedTrack == 2 ){
LoadAfterDelay("UnderPass");
}
}
Were you able to find the solution to this problem? I seem to be having the same issue when I instantiate items
Your answer
![](https://koobas.hobune.stream/wayback/20220613122540im_/https://answers.unity.com/themes/thub/images/avi.jpg)