Ok, im having quite the issue here, and I’ve searched and couldn’t find any similar posts. Im following this guide here about object labels for a level selection type thing. I’ve got this Java code here, and it works perfectly:
var target : Transform;
var offset = Vector3.zero;
private var cam : Camera;
private var thisTransform : Transform;
function Start () {
target = GameObject.Find("CubeTest").transform;
thisTransform = transform;
cam = Camera.main;
guiText.text = "test";
guiText.material.color = Color(0,1,1);
}
function Update () {
thisTransform.position = cam.WorldToViewportPoint(target.position + -1*offset);
}
But im trying to keep my whole project in C# so this is what I have:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
private Transform target;
public Vector3 offset = Vector3.up;
public Camera mainCamera;
private Transform selfTransform;
// Use this for initialization
void Start () {
target = GameObject.Find("CubeTest").transform;
selfTransform = transform;
mainCamera = Camera.main;
guiText.text = "test";
guiText.material.color = Color.green;
}
// Update is called once per frame
void Update () {
selfTransform.position = mainCamera.WorldToViewportPoint(target.position + -1*offset);
}
}
For some reason with the C# script, im getting the error “‘Camera’ does not have a definition for ‘main’” which isnt making sense to me, seeing as the Javascript worked just fine.
Can anyone shed any light on this?