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 Chronos-L · Apr 10, 2013 at 09:29 AM · c#javascriptgetcomponent

What will happen if I GetComponent in this way? (Example)

What will happen when I do this or that?

I have seen too many users getting errors and null references in GetComponent(). GetComponent() itself is pretty straight forward: get a particular component in a game object. However, there are 3 overloads for GetComponent():

  1. GetComponent( type: string )

  2. GetComponent( type: Type )

  3. GetComponent< T >()

and there are slight variations for the result between writing in C# and uJS. So, occasionally, users will hit problem when attempting to use GetComponent()

I have compiled scripts on all the possible combinations (I think I did, feel free to point out if I didn't) and test out myself what will happen when one write GetComponent() in that particular sentence/syntax.


Reminder

This is not a dicussion on which GetComponent() is faster


Extra

Explanation/example on GetComponent: http://unitygems.com/script-interaction1/

No test ran on Boo because I don't know how to write in Boo, sorry about that.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Chronos-L · Apr 10, 2013 at 09:30 AM

C#

You should not run the DataCheckCS.cs as it is, you need to comment out everything but the line/snippet that you are interested in*

DataCS.cs

 using UnityEngine;
  
 public class DataCS : MonoBehaviour {
     public int data = 1;

 }

DataCheckCS.cs

using UnityEngine;

 public class DataCheckCS : MonoBehaviour {
    
    public GameObject gameObjectWithDataCS;
    
    void Start () {
 /* 
 ===========================================================================
       Anonymous Variable/One-liner
 ===========================================================================
 */   
 
       /*___________ GetComponent( type : string ) _________________________*/
       
       // CS1061, UnityEngine.Component doesn't have `data` ... 
       gameObjectWithDataCS.GetComponent("DataCS").data = 9;
       
       
       // Okay
       (gameObjectWithDataCS.GetComponent("DataCS") as DataCS).data = 9;
       
       ((DataCS)gameObjectWithDataCS.GetComponent("DataCS")).data = 9;
       
       
       /*___________ GetComponent( type : Type ) ___________________________*/
       
       // CS0119, expression denotes a `type` ... 
       gameObjectWithDataCS.GetComponent(DataCS).data = 9;
       
       ( gameObjectWithDataCS.GetComponent(DataCS) as DataCS ).data = 9;
       
       ( (DataCS) gameObjectWithDataCS.GetComponent(DataCS)).data = 9;
       
       
       // - CS1061, UnityEngine.Component doesn't have `data` ...
       gameObjectWithDataCS.GetComponent(typeof(DataCS)).data = 9;
       
       
       // Okay
       ( gameObjectWithDataCS.GetComponent(typeof(DataCS)) as DataCS ).data = 9;
       
       ( (DataCS) gameObjectWithDataCS.GetComponent(typeof(DataCS))).data = 9;
       
       
       /*___________ GetComponent<T>() _____________________________________*/
       // Okay
       gameObjectWithDataCS.GetComponent<DataCS>().data = 9;
       
 /* 
 ===========================================================================
       Assignment
 ===========================================================================
 */    
 
       /*___________ GetComponent( type : string ) _________________________*/
       
       // - CS0226, Cannot implicitly convert type ...
       DataCS x = gameObjectWithDataCS.GetComponent("DataCS");
       
       // Okay
       DataCS x = gameObjectWithDataCS.GetComponent("DataCS") as DataCS;
       x.data = 9;
       
       // Okay
       DataCS x = (DataCS)gameObjectWithDataCS.GetComponent("DataCS");
       x.data = 9;
       
       /*___________ GetComponent( type : Type ) ___________________________*/
       // Refer to Anonymous Variable/One-liner -> GetComponent( type : Type )
       
       /*___________ GetComponent<T>() _____________________________________*/
       // Refer to Anonymous Variable/One-liner -> GetComponent<T>()
    
    }
    
 }
 



UnityScript/uJS

You should not run the DataCheck.js as it is, you need to comment out everything but the line/snippet that you are interested in

Data.js

 #pragma strict
  
 public var data : int = 1;

DataCheck.js

 #pragma strict
  
 var gameObjectWithData : GameObject;
  
 function Start () {
  
 /* 
 ===========================================================================
       Anonymous Variable/One-liner
 ===========================================================================
 */
  
     /*___________ GetComponent( type : string ) _________________________*/
  
     // Compile error: data is not member of Component
     gameObjectWithData.GetComponent("Data").data = 9;
  
     // Okay
     (gameObjectWithData.GetComponent("Data") as Data).data = 9;
  
     /*___________ GetComponent( type : Type ) ___________________________*/
  
     // Okay
     gameObjectWithData.GetComponent(Data).data = 9;
  
  
     /*___________ GetComponent<T>() _____________________________________*/
  
     // Okay
     gameObjectWithData.GetComponent.<Data>().data = 9;
  
 /* 
 ===========================================================================
       Assignment
 ===========================================================================
 */ 
 
     /*___________ GetComponent( type : string ) _________________________*/
  
     // x is considered a component, so this line is still okay
     var x = gameObjectWithData.GetComponent("Data");
     //Not okay, Compile error: data is not member of Component
     x.data = 9;
  
     // Warning: Implicit downcast from Component to Data
     var x : Data = gameObjectWithData.GetComponent("Data");
     x.data = 9;
  
     // Okay
     var x : Data = gameObjectWithData.GetComponent("Data") as Data;
     x.data = 9;
  
  
     /*___________ GetComponent( type : Type ) ___________________________*/
  
     // Okay
     var x  = gameObjectWithData.GetComponent(Data);
     x.data = 9;
  
     // Okay
     var x : Data = gameObjectWithData.GetComponent(Data);
     x.data = 9;
  
     /*___________ GetComponent<T>() _____________________________________*/
  
     // Okay
     var x = gameObjectWithData.GetComponent.<Data>();
     x.data = 9;
  
     // Okay
     var x : Data = gameObjectWithData.GetComponent.<Data>();
     x.data = 9;
 }
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

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

10 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How to use "GetComponent" and "transform" code in dll file? 0 Answers

Setting Scroll View Width GUILayout 1 Answer

Component is null? 1 Answer

Help| Convert a javascript to C# 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