[CS0266] Cannot implicitly convert type 'object' to 'float'

I’m using PhotonNetwork and the TimeofDay script, my objective is to synchronize the time between players, I think I have the right coding except for one part. Receiving and setting the float to the GameObjects Component.

using UnityEngine;
using System.Collections;

public class SyncTime : Photon.MonoBehaviour {

	public GameObject skyDome;
	float realTime;

	// Use this for initialization
	void Start () {
		realTime = GetComponent<TOD_Sky> ().Cycle.Hour;
	}
	
	// Update is called once per frame
	void Update () {
		if (photonView.isMine) {
			//Do nothing -- Character is mine
		} else {
			skyDome.GetComponent<TOD_Sky>().Cycle.Hour = realTime;
		}
	}
	
	public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
		if (stream.isWriting) {
			//Our player
			stream.SendNext (realTime);
		} else {
			//Their player
			realTime = stream.ReceiveNext(); //<------- ERROR IS HERE
		}
	}
}

realTime is a float.

stream.ReceiveNext() evidently returns an object.

The compiler doesn’t know what type of object is being returned, so its giving you this warning.

You need to manually cast it to a float:

realTime = (float)stream.ReceiveNext();

HI @JakeLeB,

I not aware much with photon network but as i read your post and answer of @SilentSin, PhotoStream.ReceiveNext() function is return object as result of functin.

I just suggest use float.TryParse() insted of force typecast

float.TryParse(stream.ReceiveNext(),out realTime);

because I don’t think stream.ReceiveNext function provide each time float value in object, some time it may be provide other kind of string like “00:15” in this case you doing force typecast to float it will again throw error, so it’s safe to use float.TryParse(), using it if there are not float string or object received it’s should convert it to value 0, and not throw any error. I also prefer to debug result of stream.ReceiveNext() befor typecast / parse it into float.