• 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
Question by Gatsua · Jan 04, 2015 at 08:30 PM · c#networkinginstantiaterpc

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.

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

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.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

25 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

Related Questions

Network.Instantiate doesn't update in Hierarchy? 0 Answers

Multiplayer Moving Bullet 1 Answer

How do i check which object was instantiated last? 1 Answer

How To Deal With Lingering Prefabs in Multiplayer Scene ? 0 Answers

Trying to send command for object without authority. 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