• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zerox911 · Mar 20, 2012 at 09:38 AM · c#javascripterrortobce0020

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#
     }
 }

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xtplpune · Mar 21, 2012 at 08:38 AM 0
Share

function RotateCamera(){

 Camera.GetComponent("SmoothLookAt").target = menu.transform;

}

try with this code.....

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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'.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zerox911 · Mar 20, 2012 at 09:55 AM 0
Share

ok.. that fixed that error . and another error appears stating :

BCE0004 : Ambiguous reference 'camera' : ReturnTo$$anonymous$$ain.camera, UnityEngine.Component.camera.

this means???

avatar image syclamoth · Mar 20, 2012 at 10:27 AM 0
Share

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.

avatar image zerox911 · Mar 20, 2012 at 10:45 AM 0
Share

hell yeah..!!!

thanks bro... appreciated it very much..

avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zerox911 · Mar 20, 2012 at 10:46 AM 0
Share

found the problem.. thanks for trying to help. still appreciate it.. ^^

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

type not found 1 Answer

The name 'TrackManager' does not exist in the current context [Unity Vuforia Ground Plane Project] 0 Answers

Trouble converting player controller from JS to C# 1 Answer

I can do this in JS or not ??? 2 Answers

Distribute terrain in zones 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges