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 Sobe459 · Dec 14, 2014 at 09:06 AM · networkphotonserverclient

Photon.Instantiate being called twice in OnloadedLevel and/or OnroomJoin

Hey all I have been working with PUN and following the Sky Arena tutorials along with the examples. I've been scratching my head over this issue for two days with out much luck. What happening is say one client creates a room, second player connects to the room, both players get spawned but the second clients prefab is instantiated twice. The client can only move the second one spawned. The first just is suspended in the air and the second client cannot see its duplicate prefab. I hope that isnt too confusing, I have images and code as well. Thanks in advanced!

 void OnJoinedRoom()
     {
         //Pause the message queue. While unity is loading a new level, updates from Photon are skipped.
         //So we have to tell Photon to wait until we resume the queue again after the level is loaded. See MultiplayerConnector.OnLevelWasLoaded
         PhotonNetwork.isMessageQueueRunning = false;
 
         MapQueueEntry currentMap = MapQueue.GetCurrentMap();
 
         Debug.Log( "OnJoinedRoom. Loading map: " + currentMap );
 
         if( currentMap.Equals( MapQueueEntry.None ) )
         {
             PhotonNetwork.LeaveRoom();
             return;
         }
 
         Application.LoadLevel( currentMap.Name );
 
         if (Application.loadedLevelName == "Lobby" || Application.loadedLevelName == "Login") {
             Debug.Log ("In Login or Lobby");
             return;
         }
         else {
             Debug.Log ("Spawn Player");
             GameObject newPlayerObject = PhotonNetwork.Instantiate ("Roboot", Vector3.zero, Quaternion.identity, 0);
             
             Debug.Log ("Set up Camera");
             RPGCamera.GetCameraTarget();
             //RPG_Camera.CameraSetup ();
             
         }
     }

alt text

fakeclient.png (324.2 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SkaredCreations · Dec 14, 2014 at 11:51 AM

I think you're missing something, first of all you shouldn't call Application.LoadLevel because as soon as Unity loads the new level it stops the execution of your script (so neither of the next line after it are called). Instead you should call PhotonNetwork.LoadLevel on your master client and then call PhotonNetwork.Instantiate in the Awake method of your client in the level to be loaded. I suggest you to take a look at the demos attached to the PUN package, for example in DemoWorker and particularly at the scripts WorkerMenu (used in the main menu scene) and WorkerInGame (used in the game scene).

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
avatar image
0

Answer by Sobe459 · Dec 15, 2014 at 03:40 AM

I looked over the Demos again, I've changed it to as close to it as I could. Still getting a phantom client owned by the second client to connect. I even added a PhotonView to the Gameobject holding the spawning script in the new scene like the worker demo. This is what I have came up with.

The callback manager script: void OnJoinedRoom() { //Pause the message queue. PhotonNetwork.isMessageQueueRunning = false;

         PhotonNetwork.LoadLevel("TestMap1");
     }
 
     void OnLevelWasLoaded( int level )
     {
         Debug.Log( "OnLevelWasLoaded: " + Application.loadedLevelName );
 
         //Resume the Photon message queue so we get all the updates.
         PhotonNetwork.isMessageQueueRunning = true;
 
         //Time is frozen at the end of a round, so make sure that we resume it when we load a new level
         Time.timeScale = 1f;
     }


The spawning script:

     void Awake () {
         if (Application.loadedLevelName == "Lobby" || Application.loadedLevelName == "Login")
         {
             //Debug.Log("In Login or Lobby");
             return;
         }
         else
         {
             Debug.Log("Spawn Player");
             GameObject newPlayerObject = PhotonNetwork.Instantiate("Roboot", Vector3.zero, Quaternion.identity, 0);
 
             Debug.Log("Set up Camera");
             RPGCamera.GetCameraTarget();
             //RPG_Camera.CameraSetup ();
         }
     }


EDIT: After adding a bunch of debug longs, it seems to be that every client that joins get instantiated twice. The awake function in the spawning script is getting called twice on each client joining, i believe this is what is causing the "phantom" client that doesnt move.

Comment
Add comment · Show 6 · 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
avatar image Dreaded-Kane · Jan 11, 2017 at 05:15 AM 0
Share

2017 and I'm also having this problem, was there ever a solution found?

avatar image ChristianSimon Dreaded-Kane · Jan 11, 2017 at 09:05 AM 0
Share

Hi,

can you share some more details? Or do you have exactly the same code as above?

avatar image Dreaded-Kane ChristianSimon · Jan 11, 2017 at 09:45 AM 0
Share

I'm calling PhotonNetwork.Instantiate in the Start method after PhotonNetwork.LoadLevel.

If I start a server, connect a client, disconnect the server (client becomes master client) and rejoin, the phantom prefab appears.

Another thing I discovered today (which could be related) is when I connect to a server as a client the Start method on my prefab is called twice. I think the prefab is instantiated once, destroyed, and instantiated again. I know this because I was parenting a DontDestroyOnLoad object to my prefab but it got destroyed (presumably when the first instantiation was destroyed).

Hopefully this makes sense :)

Show more comments
Show more comments

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

Photon - Lan? 1 Answer

Network , new player can't see the old players ? 1 Answer

Download pictures and store them temporarily 1 Answer

Network game. Server not responding(stuck). 1 Answer

Authoritative vs Non-authoritative server (turn-based) 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