Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by ODNSEV7N · Nov 23, 2013 at 08:17 AM · network.instantiate

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

alt text

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.

alt text

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"); 
            
     }
     
 }
  
ser2.jpg (114.9 kB)
serv.jpg (40.2 kB)
Comment
Add comment · Show 1
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 Dreamer123 · Jul 13, 2014 at 06:11 PM 0
Share

Were you able to find the solution to this problem? I seem to be having the same issue when I instantiate items

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Instantiate a random item at a random position 1 Answer

Network instantiate doesn't find assets on build 1 Answer

Prefab not spawning on client correctly 1 Answer

Network.Instantiate only Instantiates on one client. 0 Answers

Network.Instantiate dont instantiate object 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