Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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 OG1337OG · Sep 20, 2021 at 11:50 AM · editorphotoninstantiate prefabpunassign-variable

PUN2, other player's prefab not populating in assigned field in unity editor

The game I am working on is using Photon PUN2 for online and when I use the editor and a build to test the multiplayer as player 1 and player 2. When I check in the editor the editor's prefab is assigned as Player 1 and currentPlayer since it is the host but the build's player prefab is not assigned as Player2 or otherPlayer in the Game Manager script fields of the editor. Is the normally the case because the Player1 prefab is instantiated on the editor and the player2 prefab is instantiated by the build? Or am I just messing up the implementation? My code works to switch currentPlayer and otherPlayer between Player1 and Player2 when I drag the Player2 prefab in the hierarchy to the field Player2 and otherPlayer but this is not a workable solution to play the game. I appreciate any help, the code for the GameManager script is below.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using Photon.Pun;
 using Photon.Realtime;
 using System.IO;
 using TMPro;
 using System.Linq;
 using UnityEngine.SceneManagement;

 public class GameManager : MonoBehaviourPunCallbacks
 {

 public static GameManager instance;
 //Beastiary

 public GameObject Creature01;
 public GameObject Creature02;
 public GameObject Creature03;
 public GameObject Creature04;
 public GameObject Creature05;

 public GameObject Creature06;
 public GameObject Creature07;
 public GameObject Creature08;
 public GameObject Creature09;
 public GameObject Creature10;

 public GameObject Hercules;
 public GameObject Thor;
 public GameObject Horus;
 public GameObject KingArthur;
 public GameObject Merlin;
 public GameObject Jesus;
 public GameObject Mike;
 public GameObject God;
 public GameObject Lilith;
 public GameObject Satan;

//Players

 public GameObject Player1;
 public GameObject Player2;
 public GameObject currentPlayer;
 public GameObject otherPlayer;
 public GameObject tempPlayer;
 public int turnnumber;
 public Transform[] spawnPoints;
 
 public  void Awake()
 {
 CreatePlayer(); //Create a networked player object for each player that loads into the multiplayer scenes.
 }
 public void Start()
 {
 SpawnCreatures();
 turnnumber = 1;

 public void SpawnCreatures()
 {
  Debug.Log("Spawning Creatures");



 if(PhotonNetwork.IsMasterClient)// If Statement that checks if player is HostMaster {MASTER = (Player 1)}
  
     {
      PhotonNetwork.Instantiate(Hercules.name, new Vector3(1, 0, 1), Quaternion.identity, 0);
               PhotonNetwork.Instantiate(Thor.name, new Vector3(2, 0, 1), Quaternion.identity, 0);
                        PhotonNetwork.Instantiate(Horus.name, new Vector3(3, 0, 1), Quaternion.identity, 0);
                                 PhotonNetwork.Instantiate(KingArthur.name, new Vector3(4, 0, 1), Quaternion.identity, 0);
                                          PhotonNetwork.Instantiate(Merlin.name, new Vector3(5, 0, 1), Quaternion.identity, 0);
     }
     else// Else Statement that assigns for {CLIENT (Player 2)}
     {
      PhotonNetwork.Instantiate(Jesus.name, new Vector3(1, 0, 8), Quaternion.identity, 0);
               PhotonNetwork.Instantiate(Mike.name, new Vector3(2, 0, 8), Quaternion.identity, 0);
                        PhotonNetwork.Instantiate(God.name, new Vector3(3, 0, 8), Quaternion.identity, 0);
                                 PhotonNetwork.Instantiate(Lilith.name, new Vector3(4, 0, 8), Quaternion.identity, 0);
                                          PhotonNetwork.Instantiate(Satan.name, new Vector3(7, 0, 8), Quaternion.identity, 0);
     }

 }

private void CreatePlayer() { Debug.Log("Creating Player");

     if(PhotonNetwork.IsMasterClient)// If Statement that checks if player is HostMaster {MASTER = (Player 1)}
     {
   Player1 =  PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonPlayer"), new Vector3(0, 1, 0), Quaternion.identity);

    currentPlayer = Player1;

     }
     else// Else Statement that assigns for {CLIENT (Player 2)}
     {
    Player2 = PhotonNetwork.Instantiate(Path.Combine("PhotonPrefabs", "PhotonPlayer"), new Vector3(9, 1, 9), Quaternion.identity);

     otherPlayer = Player2;
     }
 }

 public void EndTurn()
 {
     
     tempPlayer = currentPlayer;
     currentPlayer = otherPlayer;
     otherPlayer = tempPlayer;

     turnnumber += 1;
      Debug.Log("Turn: " + turnnumber);
 }

   
 void Update()
 {
   
 }
 

}

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 Captain_Pineapple · Sep 20, 2021 at 05:49 PM

Hey there and welcome to the forum,

i can only recommend to take a pen and paper and draw a little diagramm of what exactly happens on each players side. Make no assumption of what you think your code does in that case but really draw it as you go through the code.


You will come to realise that your code basically only takes into account what happens to the player that you instantiate yourself in your instance.

A bit further explained:

When you call create player the following will happen from player1 (masterplayer) (for now called A):

  • A instantiates it's own playerObject.

  • A's object will be created as uncontrollable object on Player2s end.

  • A locally assignes the object to currentPlayer

When you call create player on player2 (for now called B):

  • B instantiates it's own playerObject.

  • B's object will be created as uncontrollable object on A's end.

  • B locally assignes the object to otherPlayer


Now A has set currentPlayer to its own playerObject and B has set Its own playerObject to otherPlayer.

You nowhere ever assign the respective other variable. So basically what i'd suggest for you would be the following: Instead of having the direct assignement make the playerobject assign itself to the correct slot when it calls Awake on the object itself.

You probably have some kind of PlayerLogic script that runs on the Instantiated player. To the awake function add something like this:

 if (PhotonNetwork.IsMasterClient)
         {
             if(photonView.IsMine())// this should check if the photonview on this object *belongs* to the localPlayer
                 GameManager.instance.currentPlayer = gameObject;
             else
                 GameManager.instance.otherPlayer = gameObject;
         }
         else
         {
             if (photonView.IsMine())
                 GameManager.instance.otherPlayer = gameObject;
             else
                 GameManager.instance.currentPlayer = gameObject;
         }

SideNote: Your code might end up confusing yourself as currentPlayer and otherPlayer are (at least from your current code) directly translateable to currentPlayer is equal to MasterClient and otherPlayer is equal to the non MasterClient. This works good from Player1's point of view but might be confusing when trying to figure out code that regards Player2 as on its own end its own playerObject is to be found in otherPlayer.

Just make sure for yourself that this is how you want things to be.

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

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

Related Questions

Photon instantiates all Network instantiated Objects again when other Players connect? 1 Answer

Timer in PUN2 1 Answer

[PHOTON PUN] Players cant shoot each other? 1 Answer

Photon PUN2, Rpc score can't correctly. 1 Answer

Rigidbody2D jitter on collision. Photon Pun 2 sync issue? 1 Answer


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