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 awalltoo · Oct 13, 2015 at 11:25 AM · networkinginitialization

Client callback after syncvars have been set

I have some server-side code that instantiates a prefab, sets some properties on it, and then spawns it on the network. On the client, I would like to run some logic to initialize some client-only components of this object. I would have expected that I could do this from OnStartClient(), but that appears to get called before the object's SyncVars have been replicated. Here is a simplified example of what I'm trying to do:

 class MyComponent : NetworkBehaviour
 {
   [SyncVar]
   public GameObject MyObj;
 
   [SyncVar]
   public int MyInt;
 }
 
 ...
 
 // Some code running on the server...
 GameObject somePrefabReference;
 
 GameObject foo = Instantiate(somePrefabReference);
 foo.GetComponent<MyComponent>().MyObj = this;
 foo.GetComponent<MyComponent>().MyInt = 42;
 NetworkServer.Spawn(foo);

 ...
 
 // Some component on the object spawned above...
 public override void OnStartClient()
 {
   var myObj = GetComponent<MyComponent>().MyObj;
   var myInt = GetComponent<MyComponent>().MyInt;
   DoSomeComplicatedLogic(myObj, myInt);
 }

Unfortunately, it appears that at the time OnStartClient() is called, the properties of MyComponent have not been replicated yet, so myObj is null. It seems reasonable that OnStartClient might be expected to run before the object has finished spawning/syncing, but if that were the case, I'd expect there to be another callback I can use to run some logic after all of the variables have been replicated. Where is the proper place for me to call DoSomeComplicatedLogic() such that I can be sure all of the variables have already been replicated.

I can't put this logic inside a sync var hook for any of the properties in MyComponent, because the logic needs to wait to run until all of the SyncVars have been replicated and I don't know which order they'll be synced in. Also, it's initialization logic and I don't necessarily want it to run if the value of that SyncVar changes later on.

Thanks for any help!

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 awalltoo · Oct 19, 2015 at 10:54 AM

Actually, it looks like I was wrong. SyncVars do get replicated before OnStartClient is called, but every component is initialized in the order that it appears on the object. So, for example if I have an object...

 Foo
 - FirstComponent
 - SecondComponent

 var foo = Instantiate(FooPrefab);
 foo.GetComponent<FirstComponent>().SomeInt = 42;
 foo.GetComponent<SecondComponent>().SomeString = "Hello world";

 class FirstComponent
 {
   public override void OnStartClient()
   {
     // This prints an empty string because foo SecondComponent hasn't been replicated yet
     Debug.Log(GetComponent<SecondComponent>().SomeString);
   }
 }

 class SecondComponent
 {
   public override void OnStartClient()
   {
     // This correctly prints 42 because FirstComponent has already been replicated
     Debug.Log(GetComponent<FirstComponent>().SomeInt);
   }
 }

This makes sense now that I understand what's going on. I just kind of wish this had been documented somewhere.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity networking tutorial? 6 Answers

You may not be connected when initializing security layer. 1 Answer

UNET. Proper way to set player's team OnServerAddPlayer. 2 Answers

how do i distribute data over a network once. 0 Answers

Inheritance and uninitialized synclists 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