Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by lukachelidze · Aug 03, 2018 at 06:20 PM · networkingmultiplayerserverside

How to access caller of command from from server?

I spawn a ship object using a command and want my player have ship as a property, but I can't access player from server.

 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Networking;
 
 namespace Assets._Scripts
 {
     public class PlayerController : NetworkBehaviour
     {
         public GameObject ShipPrefab;
         public GameObject Ship;
 
         void Start()
         {
             if (!isLocalPlayer)
                 return;
 
             CmdCreateShip(netId);
 
         }
 
         void Update()
         {
             if (!isLocalPlayer)
                 return;
 
             var shipController = Ship.GetComponent<ShipController>();
 
             if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
                 shipController.CmdAdvance();
 
         }
 
         [Command]
         private void CmdCreateShip(NetworkInstanceId id)
         {
             var ship = Instantiate(ShipPrefab);
             NetworkServer.SpawnWithClientAuthority(ship, connectionToClient);
         }
     }
 }


Comment
Add comment
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
0

Answer by Bunny83 · Aug 03, 2018 at 06:54 PM

A client can only call commands on objects they have authority over. So the object this command is sent to has to belong to the client that has send the command. So just check NetworkIdentity.clientAuthorityOwner.


If you actually sending the command from the player object of a client (which is the usual case in 90% of all cases) you can also use NetworkIdentity.connectionToClient to get the connection and NetworkIdentity.playerControllerId to get the controller ID of the owning player. Note that the "playerControllerId" is basically the index into the playerControllers list

Comment
Add comment · Show 2 · 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
avatar image lukachelidze · Aug 03, 2018 at 07:17 PM 0
Share

I understand, but can't figure out the code that will do this: (player whose connectionId == NetworkIdentity.connectionToClient).Ship = ship

could you help with this.I don't know how to use connectionId and playerControllerId to get player object.

avatar image Bunny83 lukachelidze · Aug 03, 2018 at 10:09 PM 0
Share

You are confusing a few things here. The "Ship" you talk about needs to be set on the client. Commands are methods that are executed on the server only. Since you spawn that ship with client authority you should simply attach a NetworkBehaviour to that ship that actually controls that ship. On that NetworkBehaviour on the ship you can use OnStartAuthority. This method will only be called on the client that owns the object after it has been spawned.


$$anonymous$$eep in $$anonymous$$d that every peer in a networking game has it's own set of gameobjects and every script exists on every computer again. However those scripts are seperate instances. If you want / need to exchange any data between different peers (that includes client - server) you need some sort of syncing. If clients want to talk to the server you use Commands, if the server wants to talk to clients he uses ClientRPCs or TargetRPCs. Furthermore you can use SyncVars which are automatically synchronised between sever and client.


While networking isn't blackmagic even the smallest example is rather complex (complex, not complicated). There are several objects involved, input is detected locally, events need to be send between peers, etc. $$anonymous$$ost questions about networking that get asked here only include a tiny fraction of the whole setup. However it's important to know all the details to actually understand what's happening or how the setup looks like. Individual help becomes quite difficult. If you want to start a discussion about what may be a good approach for a certain problem you may want to start a thread on the forum.

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.

Follow this Question

Answers Answers and Comments

173 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 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 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 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 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 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 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

Unity networking tutorial? 6 Answers

what is the best way to implement server side of a multiplayer game? 1 Answer

MMO server side programming 0 Answers

UNET OnCollisionEnter issues 0 Answers

UNET: Server Control and What Clients See? 0 Answers


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