- Home /
Question by
markrashera · Dec 16, 2016 at 11:48 AM ·
networkingphotonwebcamtexturewebcamgetpixels
Cannot get pixels when webcam is not running
I am trying to simply sync the current camera frame with the other person in the mutliplayer room, using photon. This is my script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class streamWebcam : Photon.MonoBehaviour
{
public Texture2D imgCam;
public Texture2D nocam;
public Texture2D receivedImage = null;
public void sendBytes()
{
WebCamTexture webcamTexture = new WebCamTexture();
if (WebCamTexture.devices.Length > 0)
{
imgCam.SetPixels(webcamTexture.GetPixels()); //<--- ERROR HERE
imgCam.Apply();
webcamTexture.Play();
}
else
{
imgCam = nocam;
imgCam.Apply();
}
photonView.RPC("bytes", PhotonTargets.All, imgCam.EncodeToJPG(), PhotonNetwork.player.NickName);
}
[PunRPC]
void bytes(byte[] received, string playername)
{
if (PhotonNetwork.player.NickName != playername)
{
print("received!");
Texture2D got = new Texture2D(640, 480, TextureFormat.RGBA32, false, false);
got.LoadImage(received);
got.Apply();
receivedImage = got;
}
}
}
But i get this error when i try to do imgCam.SetPixels(webcamTexture.GetPixels());
Cannot get pixels when webcam is not running. I searched every where and didn't find anything regarding this. Please help.
Thanks, Mark.
Comment
I guess you need to instantiate imgCam
variable before trying to access it using SetPixels(...) function.