Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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.

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

Trouble converting player controller from JS to C# 1 Answer

WebGL build is getting cached in the browser, users cannot see updates... 0 Answers

Multiple Cars not working 1 Answer

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

GetComponents array in C# error ? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges