Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 arsh24 · Mar 26 at 01:57 AM · scenedestroyphotonscene-switchingdontdestroyonload

How to switch scenes with two players using Photon?

I am making a multiplayer game in which there are two players. Both of the players use the same PlayerController script, as they are from the same prefab which has been instantiated. I want players to be able to switch scenes throughout the game, so I would either have to re-instantiate them again after a scene change or just use the DontDestroyOnLoad() method on the player, but the problem is that in order to use this method, I have to apply singleton pattern on it, which will directly contradict the instantiation and not allow multiplayer. I tried to make things work by adding Photon actor numbers to distinguish between players by applying the following code, but the problem still persists:

     public class PlayerController : MonoBehaviour
     {
     private static bool created1;
     private static bool created2;
     private int actorNum;
     public static int count;
 
     void Awake()
     {
         actorNum = PhotonNetwork.LocalPlayer.ActorNumber; //assigned 1 for Player1 and 2 for Player2
         Debug.Log("PLAYER ACTOR NUM " + actorNum);
         if (actorNum == 1)
         {  checkPlayer1();  }
         else
         {   checkPlayer2(); }
     }
       private void checkPlayer1()
     {
         if (!created1 && actorNum == 1)
         {
             DontDestroyOnLoad(this.gameObject);
             created1 = true;
             Debug.Log("PLAYER CREATED = " + created1);
         }
         else if (created1 == true && actorNum == 1)
         {
             Debug.Log(PhotonNetwork.LocalPlayer.ActorNumber + " ACTOR IS DESTROYED");
             Destroy(this.gameObject);
         }
         count++;
         Debug.Log("PLAYER1 AWAKE METHOD COUNT = " + count);
     }
     private void checkPlayer2()
     {
         if (!created2 && actorNum == 2)
         {
             DontDestroyOnLoad(this.gameObject);
             created2 = true;
             Debug.Log("PLAYER CREATED = " + created2);
         }
         else if (created2 == true && actorNum == 2)
         {
             Debug.Log(PhotonNetwork.LocalPlayer.ActorNumber + " ACTOR IS DESTROYED");
             Destroy(this.gameObject);
         }
         count++;
         Debug.Log("PLAYER2 AWAKE METHOD COUNT = " + count);
     }
  }

In the above code, I have tried to allow the script to create an instance only if the Photon player's actor number is different (either 1 or 2), which will confirm that they are different players. The same is for destroying also, which will prevent creating duplicate instances of existing players when changing scenes.

However till Player1, everything seems fine. Problem comes when Player2 is instantiated and the Awake() method runs twice. The first time it runs, it creates Player2's instance, but the second time, it destroys Player2. I am not sure what and why it is happening. Your help is much appreciated and please do tell me if there is an easier way to switch scenes in multiplayer than this.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ramisthand76 · Mar 27 at 02:29 PM

Turn Game Manager into a prefab by dragging it from the scene Hierarchy to the Assets Browser, it will turn blue in the Hierarchy. Edit GameManager Script. mobdro

myfiosgateway.one

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

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

169 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 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 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 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 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 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 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 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 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 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

Client player cannot see MasterClient player after scene change 0 Answers

DontDestroyOnLoad Slowing Loading Down 0 Answers

How to disable Dont destroy on load 0 Answers

How to make a saved score destroy on load new scene 0 Answers

How to load a scene with GameFlow on button click ? ( XR RIG / Oculus Quest ) 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