- Home /
Convert Java to C#
I have a code that I want to convert to C# but I'm having some insues on convertion
This is the Java code, that I found in this answer
#pragma strict
private var webcamTexture1 : WebCamTexture;
private var webcamTexture2 : WebCamTexture;
var target1 : Transform;
var target2 : Transform;
function Start () {
webcamTexture1 = WebCamTexture();
webcamTexture2 = WebCamTexture();
var devices : WebCamDevice[] = WebCamTexture.devices;
if(devices.length > 1){
webcamTexture1.deviceName = devices[0].name;
webcamTexture2.deviceName = devices[1].name;
target1.renderer.material.mainTexture = webcamTexture1;
target2.renderer.material.mainTexture = webcamTexture2;
webcamTexture1.Play();
webcamTexture2.Play();
}
else
print("Make sure you got two cams");
}
And this was my try:
using UnityEngine;
using System.Collections;
public class WebCam : MonoBehaviour {
private WebCamTexture webcamTexture1;
private WebCamTexture webcamTexture2;
Transform target1;
Transform target2;
void Start (){
webcamTexture1 = WebCamTexture();
webcamTexture2 = WebCamTexture();
WebCamDevice[] devices = WebCamTexture.devices;
if(devices.length > 1){
webcamTexture1.deviceName = devices[0].name;
webcamTexture2.deviceName = devices[1].name;
target1.renderer.material.mainTexture = webcamTexture1;
target2.renderer.material.mainTexture = webcamTexture2;
webcamTexture1.Play();
webcamTexture2.Play();
}
else
print("Make sure you got two cams");
}
}
Here is the list of error on Unity?
I know this has been answered. Just wanted to add, you CAN do it, but not necessary, you don't have to use 'private', because in c-sharp, variables are private by default. That means if you want them to be editable in the inspector, you must make them 'public'.
using UnityEngine;
using System.Collections;
public class WebCam : $$anonymous$$onoBehaviour
{
WebCamTexture webcamTexture1;
WebCamTexture webcamTexture2;
public Transform target1;
public Transform target2;
void Start ()
{
webcamTexture1 = new WebCamTexture();
webcamTexture2 = new WebCamTexture();
WebCamDevice[] devices = WebCamTexture.devices;
if(devices.Length > 1)
{
webcamTexture1.deviceName = devices[0].name;
webcamTexture2.deviceName = devices[1].name;
target1.renderer.material.mainTexture = webcamTexture1;
target2.renderer.material.mainTexture = webcamTexture2;
webcamTexture1.Play();
webcamTexture2.Play();
}
else
print("$$anonymous$$ake sure you got two cams");
}
}
Answer by Scribe · Jun 10, 2013 at 04:24 PM
On line 15 and 16 you need the 'new' keyword:
webcamTexture1 = new WebCamTexture();
webcamTexture2 = new WebCamTexture();
On line 20, Length should be capitalised (Length as apposed to length):
if(devices.Length > 1){
Also you won't get an error but as variables default to private in C# and public in Unityscript on line 10 and 11 you should add 'public' (otherwise they won't be exposed in the inspector) and if you wanted you could leave out 'private' on lines 7 and 8:
WebCamTexture webcamTexture1;
WebCamTexture webcamTexture2;
public Transform target1;
public Transform target2;
Scribe
Your answer
![](https://koobas.hobune.stream/wayback/20220613103915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Problem Javascript to C# 3 Answers
dealing with gameObject formations 1 Answer
how do i make teams for a rts game 1 Answer