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 ZorNiFieD · Dec 26, 2012 at 10:33 PM · c#getcomponentcomponent

Why is reference to script object == null?

Greetings;

In C#, I have the following script:

 TweenRotation _tweenRotation;
     void Start() {
         _tweenRotation = GetComponent<TweenRotation>();
         if (_tweenRotation == null) {
             print ("can't find _tweenRotation");
         }
     }

     void OnClick_btnSettings(){
     // this works just fine
     _tweenRotation.enabled = true;
 }

This returns null. However, I can access the script just fine. The documentation from the following URL states this:

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

"function GetComponent (type : Type) : Component Description Returns the component of Type type if the game object has one attached, null if it doesn't. You can access both builtin components or scripts with this function."

Why is the object comparable to null if the object is valid and I can access the object just fine from within code?

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 Bunny83 · Dec 26, 2012 at 11:22 PM 1
Share

Well, I would guess that the TweenRotation class might implement the == operator and the equals function. That way you can pretend that the object doesn't exist. That's usually done for internal types that you shouldn't be able to access. However it's not a nice workaround.

Have you tried to access the reference right after the GetComponent call?

2 Replies

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

Answer by clunk47 · Dec 26, 2012 at 10:41 PM

Try something like this. Check if the Component is found before assigning it.

     TweenRotation _tweenRotation;
 
     void Start() 
     {
         if(GetComponent<TweenRotation>())
            _tweenRotation = GetComponent<TweenRotation>();
         
         else
             print ("can't find _tweenRotation");
     }
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 ZorNiFieD · Dec 26, 2012 at 11:14 PM 0
Share

This gives the same result but because the assignment is hidden in the true clause, now I can't use the objects functions. I'm still confused as to why the object is returning as null but also usable at the same time. There is definately a C# script attached to this game object titled TweenRotation when this code is called.

avatar image ZorNiFieD · Dec 26, 2012 at 11:19 PM 0
Share

Ok, I'm confused. I can access the script functions using just the delcaration of type:

 TweenRotation _tweenRotation;

However, the program prints "can't find _tweenRotation". So I'm sure that

 _tweenRotation = GetComponent<TweenRotation>();

is never called.

Why is the line in my buttons OnClick still working?

 _tweenRotation.enabled = true;
avatar image Lovrenc · Dec 26, 2012 at 11:36 PM 0
Share

Well, for instance in javascript i think this would work on uninitialized variable. So maybe ins$$anonymous$$d of declaring a property try reading it. This will tell you for sure wheather you have reference or not.

avatar image
1

Answer by Bunny83 · Dec 27, 2012 at 12:12 AM

Well, it's also possible that you assign the variable elsewhere. To find out where your _tweenRotation get assigned, just turn it temporary into a property:

 TweenRotation internal_TR = null;
 TweenRotation _tweenRotation
 {
     get{ return internal_TR; }
     set
     {
         Debug.Log("new value :" + value);
         Internal_TR = value;
     }
 }

Now you can see in the console how often a new value is assigned and thanks to the stack trace you see from where it's assigned.

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

12 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

Related Questions

Inheritance vs RequireComponent -1 Answers

C# GetComponent, component not turning off. 2 Answers

Save a Script Component as a variable. 1 Answer

How to access the interface from the last component in the inspector 2 Answers

Problem with getting component, help please? ^^' 2 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