- Home /
How to send a gameobject in the OnSerializeNetworkView()
Hello, I'm a beginner with network. i have a multiplayer scene. each player can have a "Target", the target is another gameobject (another player, or a environment élément).
But, when i want to send the player script parameters on the network with OnSerializeNetworkView, i cannot send his target.
per exemple : If I send a integer "health" , it work fine
> public int currentHealth; void
> OnSerializeNetworkView(BitStream
> stream, NetworkMessageInfo info) {
> int health = 0;
> if (stream.isWriting)
> {
> health = currentHealth;
> stream.Serialize(ref health);
> }
> else
> {
> stream.Serialize(ref health);
> currentHealth = health;
> } }
But when i send a transform, or gameobject, it dont work
public Transform currentTarget;
void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
{
Transform target ;
if (stream.isWriting)
{
target = currentTarget;
stream.Serialize(ref target);
}
else
{
stream.Serialize(ref target);
currentTarget = target;
}
}
http://docs.unity3d.com/Documentation/ScriptReference/BitStream.Serialize.html i read tere, i can only use bool, char, short, int, float, Quaternion, Vector3 and NetworkPlayer
So, how can i send my player target, if i cannot send Gameobject, of transform.. ? I tried to send the gameobject ID, but i canot getting the gameobject back with it.
I have a vague answer for you; more like a nudge in the right direction... You need to have each targettable object identifiable by both (all) computers. This can be done by having the same ID number on each object on each computer or a distinctive name, which can then be found. These values can be sent in OnSerializeNetworkView... however an RPC call might be better suited for this case, since OnSerializeNetworkView is called, by default, 15 times per second.
Sorry for bumping this question. I also want to know why it doesn't work like what the thread starter asked. And I also want to know how to send custom class or custom variable using OnSerializeNetworkView.