Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 zachypin · Jun 27, 2011 at 07:52 PM · getcomponentcomponentreference

Reference Not Set toInstance of Object

So I have a JS making a call to a CS script using GetComponent.
JS:

 public static var upbutton : UpButton;
 
 function Update () 
 {
     upbutton = GetComponent(UpButton);
     upbutton.WasUpPressed(); //error here

Then there's the CS
CS:

 public class UpButton : MonoBehaviour 
 {
     public static bool upPressed = false;
             
     void OnMouseDown()
     {
         upPressed = true;
     }
             
     public bool WasUpPressed()
     {
         return upPressed;
     }
 }

Problem is in the JS error line "upbutton.WasUpPressed();" the error says
"Object reference not set to an instance of an object"

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
Best Answer

Answer by GuyTidhar · Jun 27, 2011 at 08:02 PM

In this case, it means:

 GetComponent(UpButton);

returned null, thus upbutton is null and thus you can not call the WasUpPressed function.

Do you actually have the component "UpButton" attached to the same game object you have the JS on?

Also, when you do this:

 public static var upbutton = false;

You probably make upbutton a boolean variable. You must have meant doing this:

 public static var upbutton;

Since you do not know what the variable is going to be, as you want to use duck typing.

But you should prefer doing this:

 public static var upbutton : UpButton;

letting the compiler know before hand what you meant your variable to be and avoiding surprises during run time.

Comment
Add comment · Show 6 · 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 zachypin · Jun 27, 2011 at 08:36 PM 0
Share

I actually did have: "public static var upbutton : UpButton;" $$anonymous$$is-write in code above, sorry (edited and fixed now). Yes I have the UpButton attached to the same gameObject. It is on a different hierarchy level of the gameObject though, not sure if that matters.

avatar image GuyTidhar · Jun 27, 2011 at 08:38 PM 0
Share

Different hierarchy ? $$anonymous$$aybe you mean you have it on a child game object?

avatar image zachypin · Jun 27, 2011 at 08:44 PM 0
Share

It is on a child. I have a gameObject "$$anonymous$$enu". This has animations on it. Its child is "$$anonymous$$enuItems". This tells its parent to do the animations. It only tells it though if its children, UpButton or DownButton tell it they have been pushed.

To be clear. The JS and CS are not both viewable in the same inspector, though they are both attached to the same gameObject. Is it only possible to communicate between codes if they are both in the same inspector? Seems unlikely to me.

avatar image zachypin · Jun 27, 2011 at 08:45 PM 0
Share

I even changed the function WasUpPressed to ins$$anonymous$$d return true ins$$anonymous$$d of the boolean value. Same error.

avatar image GuyTidhar · Jun 27, 2011 at 08:51 PM 1
Share

A child is not the same GameObject. The inspector is just a window - it shows the data attached to one game object at a time. Each game object has its own name, so if you place a new game object under an existing one in the hierarchy, it means you have a parent GameObject and a child GameObject. You can use this to find components in child game objects:

GetComponentInChildren(UpButton)

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Simple question about OnMouseButtonAsUp 1 Answer

Multi similar script Components same game object 1 Answer

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

Can I access a script referenced in another script (without getcomponent)? 5 Answers

Using GetComponent in multiple scripts for same component? C# 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