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 ThiagoPoa · Feb 25, 2013 at 08:41 PM · c#getcomponent

I cant get script of a GameObject(Camera)

Hi, guys

Im trying to get a script of a GameObject(Camera) like this:

 private RebeccaCam rebeccaCam;
 ...
 ...
 ...
 
 void Start(){
    this.rebeccaCam =    GameObject.Find("camera").GetComponent("RebeccaCam") as RebeccaCam;
 }

...but always return null...why?

The hierarchy:

player (prefab) -> camera (Camera)

Thx a lot...Im noob yet Thiago

Comment
Add comment
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

6 Replies

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

Answer by AlucardJay · Feb 26, 2013 at 03:53 AM

  • I think your keyword this may be the problem, try removing that first.

  • Also you can GetComponent by type not string : GetComponent (); // already typecast to specific component RebeccaAim

  • As another suggestion, break down your Find and GetComponent to see where it is failing :

.

 void Start() 
 {
     GameObject rebeccaAimObject = GameObject.Find("REBECA_CHAR");
     if ( rebeccaAimObject )
     {
         rebeccaAim = rebeccaAimObject.GetComponent < RebeccaAim > ();
         
         if ( !rebeccaAim )
         {
             Debug.Log( "REBECA_CHAR was found but the RebeccaAim Component WAS NOT ...." );
         }
     }
     else
     {
         Debug.Log( "REBECA_CHAR NOT FOUND ...." );
     }
     
     
     GameObject rebeccaCamObject = GameObject.Find("camera");
     if ( rebeccaCamObject )
     {
         rebeccaCam = rebeccaCamObject.GetComponent < RebeccaCam > ();
         
         if ( !rebeccaCam )
         {
             Debug.Log( "camera was found but the RebeccaCam Component WAS NOT ...." );
         }
     }
     else
     {
         Debug.Log( "camera NOT FOUND ...." );
     }
 }
 

This is a very long-winded Debug, but should show where the Find and GetComponent is failing.

Comment
Add comment · Show 10 · 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 ThiagoPoa · Feb 26, 2013 at 04:21 AM 0
Share

Hi, guys thx for your help...Look I code this:

 GameObject mainCamera = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
         if(mainCamera){
             Debug.LogWarning("$$anonymous$$ain Camera is not null");    
             rebeccaCam = mainCamera.GetComponent("RebeccaCam") as RebeccaCam;
             if(rebeccaCam){
                 Debug.LogWarning("RebeccaCam is not null");    
             }else {
                 Debug.LogWarning("RebeccaCam is null");    
             }
                 
         } else {
             Debug.LogWarning("$$anonymous$$ain Camera is null");    
         }

...and the Log:

alt text

...and still doesent work...I´ll kill myself !!! :p

avatar image AlucardJay · Feb 26, 2013 at 04:34 AM 0
Share

C# is not my native language, fixed the answer for C# version of typecast GetComponent. This problem is a real headscratcher. Crazy suggestion, try changing Start to Awake for a quick test (in case the camera is not yet initialized).

avatar image ThiagoPoa · Feb 26, 2013 at 04:48 AM 0
Share

...does not work. I will write the code in javascript...but make no sense to me do this.

Thx guys a lot !!! And sorry about that

avatar image ThiagoPoa · Feb 26, 2013 at 12:39 PM 0
Share

Ok, guys...i wrote the code in javascript language and solve the problem.

 function Start () {
         
         var cam : GameObject = GameObject.Find("camera");
         
         if(cam){
             Debug.LogWarning("$$anonymous$$ain Camera is not null");    
             rebeccaCam = cam.GetComponent(RebeccaCam);
             if(rebeccaCam){
                 Debug.LogWarning("RebeccaCam is not null");    
             }else {
                 Debug.LogWarning("RebeccaCam is null");    
             }
                 
         } else {
             Debug.LogWarning("$$anonymous$$ain Camera is null");    
         }
     }


Thank you, guys for your time and patience!!

avatar image AlucardJay · Feb 27, 2013 at 03:48 AM 0
Share

Really you shouldn't have to change languages just to use GetComponent. I have done some searching today and there may be another way :

  • http://answers.unity3d.com/questions/31958/i-cant-understand-the-syntax-of-getcomponent-in-c.html

  • http://forum.unity3d.com/threads/9343-How-to-use-GetComponents%28%29-in-C

the last post on the second link shows :

 // uJS
 var nextGO : GameObject = GetComponent(someScript).nextGO;

 // C#
 GameObject nextGO = ((someScript)GetComponent(typeof(someScript))).nextGO;

again, C# is not my native so unable to provide info on using typeof(foobar)

note: this question is not solved (I have removed this), future readers wanting to know how to use GetComponent in C# will search and expect an answer if this is found. Please don't edit the title as Solved anyway, accept correct answers by clicking on the grey tick to the left of an answer. http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

Show more comments
avatar image
1

Answer by $$anonymous$$ · Feb 25, 2013 at 08:57 PM

A much better way to get a script would be to create a variable of the script and assign it in the inspector

 public RebeccaCam scriptRebeccaCam;

you can acces it by just doing:

 someVariable = scriptRebeccaCam.somePublicVariable;

look here for more on accessing other game objects

Comment
Add comment · 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
0

Answer by numberkruncher · Feb 26, 2013 at 12:45 AM

Does the following work for you?

 this.rebeccaCam = Camera.main.GetComponent<RebeccaCam>();
Comment
Add comment · Show 5 · 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 ThiagoPoa · Feb 26, 2013 at 01:03 AM 0
Share

No, doesnt work !

I create javascript, c# class...but still does not working. I want get the script to change camera´s position to right to the left side of my character.

Sorry, Im noob...but its look so easy !!! ..its killing me ! :p

avatar image numberkruncher · Feb 26, 2013 at 01:47 AM 0
Share

Is RebeccaCam definitely added to your camera object?

avatar image ThiagoPoa · Feb 26, 2013 at 03:11 AM 1
Share

alt text

95alt text95

avatar image numberkruncher · Feb 26, 2013 at 03:26 AM 0
Share

Does your camera have the $$anonymous$$ainCamera tag? That would be needed for my above suggestion to work.

avatar image ThiagoPoa · Feb 26, 2013 at 03:53 AM 0
Share

I tagged to $$anonymous$$ainCamera...and still doesnt work...sorry guys, I dont know what im doing wrong...and thank you, numberkruncher !!!

avatar image
0

Answer by fafase · Feb 27, 2013 at 07:21 AM

In C#:

 RebeccaCam  script;
 void Start(){
    Camera cam = (Camera)FindObjectOfType(typeof(Camera));
    script = cam.GetComponent<RebeccaCam>();
 }

That will work if you only have one camera if you have many you need to find the exact one with:

 RebeccaCam  script;
 void Start(){
    GameObject cam = GameObject.Find("CamName");
    script = cam.GetComponent<RebeccaCam>();
 }

Or if you are looking for the Main Camera which is named as such by default:

 RebeccaCam  script;
 void Start(){
    script = Camera.main.GetComponent<RebeccaCam>();
 }

EDIT: I just saw you want to move your camera,fetch the Transform instead

 Transform camTr;
 void Start(){
     camTr = Camera.main.GetComponent<Transform>();
 }
 void LateUpdate(){
     camTr.position = transform.position; 
 }
Comment
Add comment · 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
0

Answer by mewhy · Mar 04, 2013 at 08:27 PM

i had a similar problem and you dont have to change the code just look at this documentation. http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html

Comment
Add comment · 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
  • 1
  • 2
  • ›

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

17 People are following this question.

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

Related Questions

How to get a variable value from another script(C#)? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Using GetComponent in multiple scripts for same component? C# 2 Answers

Object Reference not Set to an Instance of an Object in C# (Closed) 1 Answer


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