- Home /
what's wrong with this??
hi there, im converting a script from C# to javascript.
the problem here is there is an error stated : BCE0020 : An instance of type 'UnityEngine.Component is required to access non static member 'GetComponent'.
any help is appreciated.. ^___^
here are the scripts.
JAVASCRIPT
#pragma strict
var camera : GameObject;
var menu : GameObject;
var SelectSound : AudioClip;
var SelectDownSound : AudioClip;
function Start(){
camera = GameObject.Find("Main Camera");
menu = GameObject.Find("Menu");
}
function OnMouseEnter(){
audio.clip = SelectSound;
audio.Play();
}
function OnMouseOver(){
renderer.material.color = Color.blue;
}
function OnMouseExit(){
renderer.material.color = Color.white;
}
function OnMouseDown(){
audio.clip = SelectDownSound;
audio.Play();
RotateCamera();
}
function RotateCamera(){
Camera.GetComponent(SmoothLookAtJS).target = menu.transform;// error in JS
}
C# SCRIPT
using UnityEngine;
using System.Collections;
public class BackToMenu : MonoBehaviour
{
public GameObject camera;
public GameObject menu;
public AudioClip SelectSound;
public AudioClip SelectDownSound;
void Start()
{
camera = GameObject.Find("Main Camera");
menu = GameObject.Find("Menu");
}
void OnMouseEnter()
{
audio.clip = SelectSound;
audio.Play();
}
void OnMouseOver()
{
renderer.material.color = Color.blue;
}
void OnMouseExit()
{
renderer.material.color = Color.white;
}
void OnMouseDown()
{
audio.clip = SelectDownSound;
audio.Play();
RotateCamera();
}
void RotateCamera()
{
camera.GetComponent<SmoothLookAt>().target = menu.transform;// no error on C#
}
}
function RotateCamera(){
Camera.GetComponent("SmoothLookAt").target = menu.transform;
}
try with this code.....
Answer by syclamoth · Mar 20, 2012 at 09:47 AM
Simple- you mis-spelled 'camera'.
You put a capital c on it-
Camera
instead of lower-case
camera
'Camera' refers to the static 'Camera' class, whereas 'camera' will return a reference to the first found 'Camera' object connected to the same Object. You need to use the 'camera' version to have access to functions that don't make sense without an instance- such as 'GetComponent'.
ok.. that fixed that error . and another error appears stating :
BCE0004 : Ambiguous reference 'camera' : ReturnTo$$anonymous$$ain.camera, UnityEngine.Component.camera.
this means???
Well, that one is a problem that would exist in both scripts, I should think. Just replace the word 'camera' with 'myCamera' or something. It's because there's the reference
Component.camera
that I mentioned earlier, and the local reference
camera
in your script. The two are conflicting.
Answer by mkovac · Mar 20, 2012 at 10:28 AM
Camera.GetComponent(SmoothLookAtJS).target = menu.transform;
use small "c" (camera.GetComponent()), as otherwise you are addressing Camera class rather than camera member
found the problem.. thanks for trying to help. still appreciate it.. ^^