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 OliverTheLove · Aug 06, 2018 at 06:18 PM · networkingmultiplayercommand

Command Function updates local SyncVar variable late on client

I have got a project where each character in the game has a target which they can cast spells upon. I also want the other players in the game to be aware of which target every player is currently targeting at the moment (think WoW, target of target).


I am currently struggling with the networking part for each client. The code I have written works well for the host, but for the client, it takes a few updates before one's target has been set. Below, I have pasted two pictures illustrating my code, cut away some unnecessary parts that aren't crucial to the problem.


This is the simple part of a Player connection script, which on connecting, instantiates a character prefab and gets authority over it.

 void Start()
 {
     if (!isLocalPlayer)
         return;
 
     CmdSpawnCharacter();
 }
 
 [Command]
 private void CmdSpawnCharacter()
 {
     GameObject go = Instantiate(myCharacterPrefab, this.transform);
 
     NetworkServer.SpawnWithClientAuthority(go, connectionToClient);
 }


The next part of code is part of my Character Script. I have understood that I can set the target variable as a Syncvar, so that the server automatically updates which target is being targeted by that character for each client on the server. So whenever I left mouse click on a targetable target (private void RaycastNewTarget()), I need to tell the server that I am switching target. Hence the function CmdSetTarget().

(All targets that are targetable have a Network Identity, so I can pass a GameObject as variable to Cmd function)


If I have hit a target, the function CmdSetTarget() prints out the true if-statement, but on the third line in the if-statement of RaycastNewTarget() I get an error that myTarget is null. However, if I try again straight after, it will work, since myTarget has been set to what I tried clicking the first time. It seems as if the server updates my own target as if it was a client as well, a few frames later.

 [SyncVar]
 private GameObject myTarget;
 
 void Update()
 {
     if (!hasAuthority)
         return;
         
     if (Input.GetMouseButtonDown(0))
     {
         RaycastNewTarget();
     }
 }
     
 private void RaycastNewTarget()
 {
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     LayerMask layerMask = LayerMask.GetMask("Targetable");
 
     if (Physics.Raycast(ray, out hit, 100.0f, layerMask))
     {
         CmdSetTarget(hit.collider.transform.gameObject);
 
         myTarget.GetComponentInChildren<Projector>().enabled = true;
 
         SetTargetHUD();
     }
 }
     
 [Command]
 private void CmdSetTarget(GameObject aTarget)
 {
     myTarget = aTarget;
 
     if (myTarget)
         Debug.Log("Target is: " + myTarget.name);
     else
         Debug.Log("Target is null now!");
 }


I am a bit unsure how I should solve this problem. Should I set myTarget to the raycast hit first, and then afterwards alert the CmdSetTarget() again, basically doing the same code? Or should I rewrite it to another way?


Hope it was clear enough. Cheers :)

target.png (31.0 kB)
start.png (11.3 kB)
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
1

Answer by Bunny83 · Aug 06, 2018 at 09:35 PM

First of all, please do not post code as images. It makes it impossible to quote your code in an answer. It also wastes a lot of memory.


Of course it will take some time until the command is actually send to the server and until the sync event is send back to your client. However i don't quite see the issue here. It seems you just want to enable the projector locally on your client. Why don't you just use the object reference you already have? If you don't want to do that for some reason you can use a ClientRPC or TargetRPC from your command method on the server to basically get a "callback" when the command has been processed. Though keep in mind that syncvars can actually define a hook which is executed when the variable is updated on that client.

Comment
Add comment · Show 1 · 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 OliverTheLove · Aug 06, 2018 at 10:10 PM 0
Share

Thanks for the reply! I changed the images to code ins$$anonymous$$d, my bad on that part.

Well there is a bit more that I use the Target for, in the function*SetTargetHud()* for example, I use myTarget a lot.

     GameObject target = hit.collider.transform.gameObject;
     myTarget = target;
     CmdSetTarget(target);

I've tried this and it works, but it feels... dirty, somehow. I could give The Syncvars Hook a bit more thought. I did give it a quick go without luck, but I could try be more persistent. (Think myTarget caused several more issues in other functions)

The TargetRPC, as I've understood is a targeted server call to a client, which would work, e.g. as a whisper or private message to one of the clients. Would this not be the same issue as with the Command function, that I must wait until the server has sent the call?

Also, would my Cmd function need to be a ClientRPC in order for the other clients to see myTarget? Or has the Syncvar already solved that issue?

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

176 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 avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

UNET : SyncVar value not updating, hooks firing 1 Answer

Commands being called with the host, but not a client. 1 Answer

Send Commands from an Object in Multiplayer 2 Answers

[Networking]How to call a command function from a UI element(a button) 2 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