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 Caps · Apr 27, 2011 at 08:04 PM · bugsendonserializenetworkview

OnSerializeNetworkView NOT sending a bool value when button is pressed???

Is this supposed to be normal?

I just made a simple test and it seems OnSerializeNetworkView is not sending a bool value when a button is pressed. I've checked all variables and everything is ok because I switched back to RPC and it sends the bool value just fine.

Could someone please check this with me?

using UnityEngine; using System.Collections;

public class Character_Network_Script : MonoBehaviour { public bool this_is_client = false; public bool test; public bool received_test; public bool test_result;

 void OnNetworkInstantiate(NetworkMessageInfo info) 
 {
     if (networkView.isMine)
     {
         this_is_client = true;
     }
 }

 void Update ()
 {
     test = false;
     received_test = false;
     test_result = false;
     if (this_is_client)
     {
         if (Input.GetKey (KeyCode.Alpha5))
         {
             test = true;
         }
     }
 }

 void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) 
 {
     if (stream.isWriting) 
     {
         stream.Serialize(ref test);
     }
     else
     {
         stream.Serialize(ref received_test);
         test_result = received_test;
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Apr 27, 2011 at 10:47 PM

I think your problem is your Update method. Every instance is setting test to false, but only the owner can set it to true. If the others receive a network update test will turn true, but in Update it get set back to false. I would use such an Update function:

void Update ()
{
    if (this_is_client)
    {
        test = Input.GetKey (KeyCode.Alpha5);
    }
}

I don't even get what's test_result is good for? And received_test doesn't need to be a member variable. That's how i would sync the test variable:

void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info) 
{
    bool tmpTest = test;
    stream.Serialize(ref tmpTest);
    test = tmpTest;
}
Comment
Add comment · Show 4 · 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 Caps · Apr 27, 2011 at 11:16 PM 0
Share

Bunny, thank you for your answer... Sorry if I'm way off here, I'm no experienced programmer. The idea here is: Only Player owner of this script/object can set test to true, if not, update keeps this value false. On the other hand, if not owner, that is, it's an instance of this object running in another client, then receive value for test via serializenetworkview. I've made these variables public members so I can watch their values in the inspector running Unity.

avatar image Caps · Apr 27, 2011 at 11:18 PM 0
Share

in your code, if player doesn't push key, or pushes it and releases it, how will value return to false?

avatar image Bunny83 · Apr 28, 2011 at 12:04 AM 0
Share

In my script on the owners PC in Update test get set to the state of the key. Get$$anonymous$$ey returns a bool that is true when the key is down and false if not. The owner is the only one who can send data. Thats why you don't need to check for isWriting/isReading in my case. On the owner OnSerializeNetworkView gets called with isWriting so the real value of test gets copied into tmpTest and get serialized. The last line is irrelevant for the owner since Serialize don't change the value. On the other players Serialize will set tmpTest to the received value and the last line updates the actual variable.

avatar image Caps · Apr 28, 2011 at 03:45 AM 0
Share

Thanks again, what's happening is that server can see this variable value change watching other clients, but clients can't see this change watching other clients. I still don't get it.

avatar image
0

Answer by Caps · Apr 28, 2011 at 09:29 PM

Just did some final tweaks and it works now.

Thank you very much for your help! :D

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

No one has followed this question yet.

Related Questions

Space Invaders style game bug. 1 Answer

BUG? Camera rotation on x set to 90! 0 Answers

Unity physics bug? 3 Answers

Bug. Player can't move. 3 Answers

Two Objects Touching??? 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