- Home /
network.instantiating players using RPC calls
Hi, I am trying to instantiate two players, one for the server and one for the client. The objects does get instantiated but I am having problem at setting each objects attributes.
First when I start the level I call this:
[RPC]
public void StartLevel()
{
currentCharacter = Camera.main.GetComponent<PlayerCreator>().currentCharacter;
level = 4;
networkView.RPC("NetworkPlayerInitializer", RPCMode.AllBuffered);
// NetworkPlayerInitializer();
multiplayerRoom.GetComponent<MultiplayerRoom>().LoadGameUI();
Application.LoadLevel(level);
}
It is when we're calling NetworkPlayerInitializer
that we're having problems. That method looks like this:
[RPC]
private void NetworkPlayerInitializer()
{
player = Network.Instantiate(player, new Vector3(0, 0, 0), Quaternion.identity, 0) as GameObject; // instantiate player object
player.tag = "Player";
player.name = "Player";
// enable components
player.GetComponent<SpriteRenderer>().enabled = true;
player.GetComponent<BoxCollider2D>().enabled = true;
player.GetComponent<PlayerProperties>().enabled = true;
player.GetComponent<AudioSource>().enabled = true;
player.GetComponent<PlayerMovement>().enabled = true;
// assign selected classes stats to player object
player.GetComponent<PlayerProperties>().characterClass = currentCharacter.characterClass;
player.GetComponent<PlayerProperties>().maxHealth = currentCharacter.characterHealth;
player.GetComponent<PlayerProperties>().currentHealth = currentCharacter.characterHealth;
player.GetComponent<PlayerProperties>().attackDamage = currentCharacter.characterBaseDamage;
player.GetComponent<PlayerProperties>().attackSpeed = currentCharacter.characterAttackSpeed;
player.GetComponent<PlayerProperties>().attackRange = currentCharacter.characterAttackRange;
player.GetComponent<PlayerProperties>().runSpeed = currentCharacter.characterRunSpeed;
}
Now in my head, this is upposed to create two player object, and it does, and change them to have the properties as out local currentCharacter
hold on both machines. But only one objects gets edited before the client get these errors:
-On this line player.GetComponent()
it says that NullReferenceException: Object reference not set to an instance of an object
. But why? Shouldn't the player
be the object of the machine editing it? Are we not only instantiating OUR OWN player on all connected machines and then the player
variable will always hold MY player object?
-It says The object PlayerTemplate(Clone) must be a prefab in the project view.
but it is, maybe the "(clone)" add is causing problems? Why am I getting this?
-At last the client says that player.tag = "Player";
is not an instance of an object.
This while on the server I get:
-View ID AllocatedID: 0 not found during lookup. Strange behaviour may occur
-`Could't invoke RPC function 'NetworkPlayerInitializer' because the networkView 'AllocatedID: 0' doesn't exist`
Both on unknown locations in the code.
Any thoughts about this? It may be many errors here but I think they are coming from the same issue, my RPC confusion.
Answer by Gatsua · Jan 05, 2015 at 02:01 PM
My problem was easy, it was because I was replacing "player" with the newly instantiated prefab each time. By making a new local variable for the new player, it got rid of the errors but it did not solve the entire problem.