Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Nikke · Feb 08, 2011 at 03:07 PM · instantiatenetworkclientsynchronize

When using Network.instantiate, how to set an additional setting for all clients (such as a script variable) ?

Hi,

When using Network.instantiate, how would one define an additional setting?

For instance, if I were to spawn a gun that has 4 bullets inside, I would call:

GameObject tempGun = ((Transform)Network.Instantiate(Gun, _targetSpawnLocation, Quaternion.identity, 0)).gameObject;

On the local code I write: (The gun has a script attached called "GunInfo")

tempGun.GetComponent<GunInfo>().Bullets = 4;

However, not all clients know how many bullets the gun has. How do I synchronize this with them?

An extra note: It would be possible using OnSerializeNetworkView(), but this runs every Update() and is completely unnecessary. It only needs to run once initially so all clients know how many bullets the gun has.

I suppose it's possible using an RPC, but how do I identify the object on all clients? How do I get the network-instantiated gun object on all clients code-wise?

Thanks, Nick

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 · Feb 08, 2011 at 03:40 PM

I guess the object you instantiate have a NetworkView attached? If so it's quite easy. RPC calls are send to the objects with a NetworkView with the same ViewID.

public class GunInfo : MonoBehaviour { private int m_Bullets = 0; public int Bullets { get { return m_Bullets; } set { m_Bullets = value; if (networkView.isMine) networkView.RPC("RPC_SetBullets",RPCMode.Others,m_Bullets); } }

 [RPC]
 void RPC_SetBullets(int aBullets)
 {
     m_Bullets = aBullets;
 }

}

Thanks to the property you don't have to worry about syncing.

If you don't have a NetworkView on your object you have to implement some kind of object management that keeps a list of all objects and using own IDs to find the corresponding objects.

Do you use an authoritative server setup? Or a "normal mixed" setup where clients can create objects? The only important thing regarding my solution is to keep in mind that only the player that created the object can sync the bullet count.

Comment
Add comment · Show 6 · 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 PrimeDerektive · Feb 08, 2011 at 03:46 PM 0
Share

Bunny's got the right solution here. $$anonymous$$y question is, why do clients care how many bullets other clients have?

avatar image Nikke · Feb 08, 2011 at 03:58 PM 0
Share

Thanks Bunny, I'll try it out!

Derek, the gun was just an simple example I thought about, so I wouldn't have to explain my own complex situation ;)

avatar image Nikke · Feb 08, 2011 at 04:19 PM 0
Share

What is the networkView.is$$anonymous$$ine for? Isn't the networkView always a component of the current gameobject it is attached to?

avatar image Bunny83 · Feb 08, 2011 at 04:45 PM 0
Share

is$$anonymous$$ine is just a easier way to check the owner (creator) of that NetworkViewID. It's the same as (networkView.viewID.is$$anonymous$$ine) or (networkView.viewID.owner == Network.player)

avatar image PrimeDerektive · Feb 08, 2011 at 04:50 PM 0
Share

What bunny said. The owner part is not referring to the object that owns it, but rather the network player that owns it (if you're using network.instantiate, the network player that called network.instantiate).

Show more comments

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

No one has followed this question yet.

Related Questions

How can I spawn a GameObject on all players in my multiplayer game? 0 Answers

Network.Instantiate and the Trusted Client problem 0 Answers

Network.instantiate for specified clients? 1 Answer

Network.Instantiate fired from client AND server 1 Answer

Network.Instantiate only Instantiates on one client. 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