• 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

· Add your 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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 Inventory System with RPCs 0 Answers

UNet - How do I make Network.Spawn not show the prefab to the user that called it? 1 Answer

Can I use Rpc Calls and Commands on the same object? 0 Answers

Networking GameManager/GameState Sync 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