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
1
Question by SonicScrew12 · Nov 18, 2017 at 08:43 PM · networkinginstantiatespawnrpccommand

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

I'm currently working on an FPS and I'm trying to network a bullet impact.

I've coded it in such a way that the bullet impact prefab is instantiated on the local client so the client can see where it has shot, which then sends a command to spawn the bullet impact prefab over the network at the same location as the client.

Here's my code:

 // Hit is from a raycast
 GameObject impact = Instantiate(impactEffect, hit.point, Quaternion.identity);
 Destroy(impact, 1.5f);

 // Prevents double-instantiation on LAN games
 if (!isServer) {
     // If you're not the host of the game, send the impact location to the host
     CmdSendServerImpactEffectLocation(hit.point);
 } else {
     // If you are the host of the game, send the impact location to the clients
     RpcSendClientImpactEffectLocation(hit.point);
 }

 [Command]
 void CmdSendServerImpactEffectLocation(Vector3 loc) {
     GameObject impact = Instantiate(impactEffect, loc, Quaternion.identity);
     Destroy(impact, 1.5f);
     NetworkServer.Spawn(impact);
 }
 
 [ClientRpc]
 void RpcSendClientImpactEffectLocation(Vector3 loc) {
     GameObject impact = Instantiate(impactEffect, loc, Quaternion.identity);
     Destroy(impact, 1.5f);
 }

The first part is wrapped in a method, obviously. What I have done here, unfortunately, doesn't work on matchmaking. Since there is no player that is the host/server, the client sees the instantiation happen twice, one locally, and one from the Network.Spawn()

Any suggestions/information about anything relating to my issue is greatly appreciated! Thank you so much! I'm also wondering, is the user that created the matchmaking room considered the host or the server?

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
Best Answer

Answer by DougRichardson · Nov 19, 2017 at 10:46 PM

Don't use NetworkServer.Spawn, since you only want to start an effect at the same location on each client but do not need to keep anything about it in sync after that.


The code changes below always send the Command to the host but include the netId of the sender. Then you can switch on that netId to determine if you were the connection who sent the original command and, if so, do nothing in that case (to prevent the prefab from being instantiated twice).


WARNING: I did not test the code at all.


 // Hit is from a raycast
 GameObject impact = Instantiate(impactEffect, hit.point, Quaternion.identity);
 Destroy(impact, 1.5f);
 
 // Send the impact location to the host
 CmdSendServerImpactEffectLocation(hit.point, netId);
 
 [Command]
 void CmdSendServerImpactEffectLocation(Vector3 loc, NetworkInstanceId senderNetId) {
     RpcSendClientImpactEffectLocation(loc, senderNetId);
 }
  
 [ClientRpc]
 void RpcSendClientImpactEffectLocation(Vector3 loc, NetworkInstanceId senderNetId) {
     if (netId != senderNetId)
     {
         GameObject impact = Instantiate(impactEffect, loc, Quaternion.identity);
         Destroy(impact, 1.5f);
     }
 }



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 SonicScrew12 · Nov 20, 2017 at 06:01 AM 0
Share

I'm sorry, but this isn't a valid answer. The code which I have posted is from a script that is disabled, since they are not the local player. For some reason when I call ClientRpc, it doesn't remotely get the NetworkInstanceId for each client, but it does it for the caller.

avatar image SonicScrew12 · Nov 20, 2017 at 03:49 PM 0
Share

Ok, I'm incredibly confused. I was messing around with the solution for a while and then decided to abandon my changes and revert back to an earlier save on my version control. All of a sudden it started working. Thanks for your help! Haha

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

122 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

Related Questions

Unity networking Command-function not being called 1 Answer

How to to spawn player, according to id? 0 Answers

Cmd is called on Server ERROR!?!! 0 Answers

Networking player input, UI, and ability use 1 Answer

Photon Network Instantiate Objects over Network 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