- Home /
How to add a component to a prefab
I am trying to add a component to a prefab but I can't get it to work. I want the component to be added only when I join a room (multiplayer). This is the code that I have right now.
var pms : PlayerMovementScript;
function OnJoinedRoom()
{ //Initialize the player and add components
player = PhotonNetwork.Instantiate("Player Capsule", Vector3.zero, Quaternion.identity, 0);
pms = player.AddComponent("PlayerMovementScript");
pms.enabled = true;
myPlayerPv = player.GetComponent(PhotonView);
}
I want it to be added only when I join the room so that I can control only my player. The component that I am trying to add is the PlayerMovementScript. Thanks.
Why not attach it to the character prefab and add a boolean variable to check whether or not they're in the room. If so, do: pms = GetComponent (Player$$anonymous$$ovementScript); pms.enabled = true;
Answer by tobiass · Jan 02, 2013 at 05:29 PM
You don't need to attach a script for this to work. You can add one but be aware that remote players won't do the same! RPCs and OnSerializePhotonView() should be defined in scripts that are active on all clients.
If you want to control only your own character, simply check the PhotonView.isMine property. This will be true for the GOs a user instantiated and also for the scene objects on the client that is the MasterClient. Only PhotonView's that belong to a player have a valid user though.
In the Marco Polo Tutorial, we use isMine and also show another way to keep control over "owned" objects: http://doc.exitgames.com/photon-cloud/Marco_Polo_Tutorial/#cat-PUN%20Tutorials
Your answer
Follow this Question
Related Questions
Photon enable/disable objects on all views? 1 Answer
trying to catch photonNetwork instantiated object of another user 0 Answers
Instantiating player when creating a new room in photon not working. 0 Answers
PUN 2 : Impossible to applied damage to all player in overlapSphere created by a bomb ? 0 Answers
Photon Instantiate 2 Answers