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 eaglemaster7 · Apr 03, 2012 at 02:32 AM · networkingraycastvariablerpcsynchronization

how to Stream.serialize(stringValue)

I know Stream.serialize does not sync string value...

actually, in my multiplayer, I want to get some variable from other's player by pointing raycast to instantiated player(remote) , the problem is variable won't syncronized between local player and instantiated remote player on other computer , and raycast getComponent returns null...

so if it impossible, please help me to figure this by RPC,in this situation (raycast selecting)? any help is appreciated.. :D

edit:

OK, Sorry.. my achievement is getting "status" directly from raycast

  1. As selector, I put this script on Camera:

      function Update(){
    
          var ray : Ray = camera.main.ScreenPointToRay(Input.mousePosition);
    
          var hit : RaycastHit;
    
          if (Physics.Raycast (ray.origin, ray.direction, hit, 100)) {
    
              ////////////reading status other player///////////////////
    
              if (hit.collider.tag=="player"){
    
              Debug.Log(hit.collider.gameObject.GetComponent("PlayerData").Status);
    
              }
    
          }
    
      }
    
    
    

2.and in player prefab i added PlayerData.js and attach Network view observed to this :

  var statusUpdated:boolean;
  var Status:String; function
  OnSerializeNetworkView(stream :BitStream, info : NetworkMessageInfo){
    if (statusUpdated==false){
       statusUpdated=true;
       stream.Serialize(this.status);     
    }
  }

and the problem still same,serializing string value on the last line.

Comment
Add comment · Show 1
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 Bunny83 · Apr 03, 2012 at 03:10 AM 0
Share

What's the code you're using? How about posting the relevant parts? Anyway syncing a string value each Network frame and using GetComponent doesn't make much sense.

You didn't tell us anything about what you want to achieve. Think about those things before you post a question. We know only the things you've posted here.

2 Replies

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

Answer by Bunny83 · Apr 03, 2012 at 12:30 PM

Never use the string version of GetComponent. The compiler can't determine the type of the returned component so it's just a Component. Use

 hit.collider.GetComponent(PlayerData).Status

Beside that have you checked if the string is synced in the inspector? Make sure you observe the script component and not the Transform component. You have to drag the script instance onto the observed variable.

edit

RPCs have to be send manually when you want to update a value. Don't send it each frame or something like that. You should send it whenever you change the status. The best way is to use a function to set a new state.

 var Status:String;
 
 function SetState(newState : String)
 {
     Status = newState;
     networkView.RPC("OnReceiveState", RPCMode.Others, Status);
 }
 
 @RPC
 function OnReceiveState(newState : String)
 {
     Status = newState;
 }

Whenever you want to change the Status variable use SetState("my state"); instead of Status = "my state";

Comment
Add comment · Show 3 · 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 eaglemaster7 · Apr 03, 2012 at 01:59 PM 0
Share

okay , actually using GetComponent is also worked nicely.

the problem is only about DATA TYP$$anonymous$$ PlayerData script even failed on comple, because serialize don't recoginize string value. it's only support bool, char, short, int, float, Quaternion, Vector3 and NetworkPlayer NOT STRING.

but when i experiencing to changing "status" to Int, it's successfully compiled and synced.

now, how about using RPC?

avatar image Kryptos · Apr 03, 2012 at 02:24 PM 0
Share

What are the string for? Can't you use an enum (cast to int during serialization)?

avatar image Bunny83 · Apr 03, 2012 at 02:57 PM 0
Share

Yes, i've just checked the docs and there's no support for strings in the BitStream class. I will add an RPC example.

avatar image
0

Answer by Kryptos · Apr 03, 2012 at 11:22 AM

Did you read http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnSerializeNetworkView.html ?

OnSerializeNetworkView should be made in two parts (reading and writing) and you cannot directly use a class variable but must use a local variable instead (it is more visible in C# where the ref keyword has to be used).

 OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) {
     var _status : String;
     if (stream.isWriting) {
         _status = this.status;
         stream.Serialize(_status);
     }
     else
     {
         stream.Serialize(_status);
         this.status = _status; 
     }
 }
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 Bunny83 · Apr 03, 2012 at 12:23 PM 0
Share

This doesn't make any difference. It sould work the way he did it. I've build a whole multiplayer hack&slash game with only one NetworkView and did it almost like that.

.Serialize will either write or read the value depending on whether it's the owner(send) or not(receive).

avatar image magnetix · Nov 09, 2012 at 11:22 AM 0
Share

I don't think String is supported by Serialize(). I think an RPC is the way to go for this.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Did not find target warning when using uNet 3 Answers

SyncVar issue 0 Answers

Is there a way to create a shared variable between all the clients over network? 1 Answer

How to sync a varible managed by the clients using UNET? 0 Answers

Send Variable. 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