Hi, I am new to photon engine
I am making a simple game with the engine
So fat it’s like this
Log in with a user name
Users can create a room in a lobby
The master player is allowed to start play
Here comes the issue
As the game scene loads, PlayerSpawner class does PhotonNetwork.Instantiate(“playerManager”……)
playerManager prefab has PhtonView of course and it has PlayerManager script at a component
In the Start() method of PlayerManager class, The following will be called
There is a public variable to hold the instance of a player
Public GameObject playerInstance;
private void Start()
{
playerInstance = PhotonNetwork.Instantiate(“player”,……)
playerInstance.transform.SetParent(a gameObject in a scene)
……
}
what’s weird is
For The non master player it works as intended for the master player, the playerManager and player gets instantiated
However, the playerInstance variable is not filled in the inspector and the setParent() did not work
Can someone help me with this ?
PhotonNetwork.Instantiate is called only on the client that … calls it. But everyone in the room will get the networked version of this and instantiates the networked object. They won’t run the code that’s below PN.Instantiate()!
With that said, it’s clear why the parenting didn’t work.
You could send an event to everyone, telling them the game starts. Then everyone calls PN.Instantiate() to create a character (which the others will see / show, too). Reparenting should be avoided or done in scripts which are on the prefab (for the object they run on)…
First Thank you for your reply
I understood the part where you say only the instantiation code will be called
(I wish I could have noticed it earlier)
I was instantiating the game objects on an NPC that has of course the photon view
I will try to have them instantiated by the players
Thank you again
May I ask you other questions?
let’s say 2(A and B) players are instantiated when through a class named PlayerSpanwer
when I start the game, the playerSpawner will call the following
private void OnEnable()
{
if (playerSpawner == null)
PlayerSpawner.playerSpawner = this;
dealer = PhotonView.Find(1).gameObject.GetComponent<Dealer>();
GameObject playerInstance = PhotonNetwork.Instantiate("player", Vector3.zero, Quaternion.identity);
playerInstance.transform.SetParent(seats[playerInstance.GetPhotonView().ViewID%3].transform);
playerInstance.transform.localPosition = Vector3.zero;
playerInstance.transform.LookAt(dealer.transform);
playerInstance.GetComponentInChildren<Camera>().enabled = true;
playerInstance.GetComponentInChildren<Canvas>().enabled = true;
}
while testing, A player will be running the game through the unity editor and B will run it through the build
the result is that on A’ side, A is seated in the intended seat but B is not
(since it’s the build on B’s side, I cannot check the details)
I believe the would be the opposite on B’s side
based on the first answer you gave me, I guess that the reason why I could at least see the other player is thanks to PhotonNetwork.Instantite() and the reason why the remote user is not seated as intended is that the rest of the codes are not called in the other player’s side.
Is this correct?
another question is about PhotonView
while searching for answers, I saw many people using
if(photonView.isMine)
{
// Do something
}
What I don’t get from this is
why do we need to check it first of all?
It gives me an impression that things on player B’s side might be called on Player A’s side
PhotonNetwork.Instantiate will locally create the game object immediately. You can access it but most changes to it are not synced automagically. So if you reparent it, this is not synced. If you GameObject.Destroy() it, this is also not happening for the other player …
IsMine is checked in code that runs on all instances of a character. Let’s say everyone instantiates a character. Others will also instantiate it, because that’s the way to show a remote player. The prefab contains a set of scripts that all instances run but … some instances are controlled by remote players. Only one is yours.
I won’t be able to explain PUN concepts in a Q&A manner here. Please refer to the docs. Read and code along the PUN Basics Tutorial, maybe.