I need to pass rgb variables to OnPhotonSerializeView.
I tried to do it like this:
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
if (stream.IsWriting)
{
stream.SendNext(healthCircle.color.r);
stream.SendNext(healthCircle.color.g);
stream.SendNext(healthCircle.color.b);
}
else
{
healthCircle.color.r = (float)stream.ReceiveNext();
healthCircle.color.g = (float)stream.ReceiveNext();
healthCircle.color.b = (float)stream.ReceiveNext();
}
}
After this i get an error:
Assets\Scripts\Player.cs(68,13): error CS1612: Cannot modify the return value of 'SpriteRenderer.color' because it is not a variable
I tried to search it in google but i didn't find anything. Sorry for my noob question. Waiting for your help :)
CodePudding user response:
SpriteRenderer.color
(as most things in Unity) is a property which either returns or takes a complete Color
as assignment.
In addition to that Color
is a struct
so a copy by value, not a reference type so even if you could do this what would happen here is
- You return the current
healthCircle.color
- On this returned
Color
value you assign e.g.r = (float)stream.ReceiveNext();
=> So this
- would always return a new
Color
instance, a copy of the current one and assign only the one component there - would have no effect since the
healthCircle.color
is never actually assigned a new value
What you want to do is rather either
var color = healthCircle.color;
color.r = (float)stream.ReceiveNext();
color.g = (float)stream.ReceiveNext();
color.b = (float)stream.ReceiveNext();
healthCircle.color = color;
or directly do
healthCircle.color = new Color((float)stream.ReceiveNext(), (float)stream.ReceiveNext(), (float)stream.ReceiveNext());