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 /
  • Help Room /
avatar image
1
Question by anmre21 · Dec 30, 2016 at 05:15 PM · javascriptgetcomponentbug-perhapsnull reference exception

Gameobject reference disappears at start

My game has objects falling and the player moves (a cauldron) to collect them and gain points. I am trying to make a Slider into a progress bar by setting its value equal to the score. I also have some text above it that specifically states the number of points.

Here is the relevant code for that:

 var progressSlider : UI.Slider;
 var pointsText : UI.Text;
 public var points : int = 0;
 public var finished : boolean = false;
 
 function Awake() {
     progressSlider = GetComponent(UI.Slider);
     pointsText = GetComponent(UI.Text);
 }
 function Update () {
     CountPoints();
 }

 function OnCollisionEnter2D (other : Collision2D) {    
     if (other.gameObject.CompareTag("B-Ingredient")) {
         other.gameObject.SetActive(false);
         points = points + 5;
     } else if (other.gameObject.CompareTag("S-Ingredient")) {
         other.gameObject.SetActive(false);
         points = points - 5;
     }
 }
 function CountPoints () {
     progressSlider.value = points; //this is the line that the NullReferenceException refers to
     if (points >= 50) {
         finished = true;
     }
     pointsText.text = "Points: " + points.ToString;
 }

Note: the collision enter stuff refers to a Rigidbody that's in the script, but I took it out to make it more focused on the problem. The collision stuff definitely isn't the problem.

I used Debug.Log and found that the references to progressSlider and pointsText are returning null, and I understand why (sorta). I assigned the slider and text their GameObjects in the inspector, but once I start the game, they disappear from the inspector: alt text alt text

I'm wondering if this is a bug. In this script (not shown in the code portion), I call for a Rigidbody and that Rigidbody doesn't disappear at the start or return null. In Play Mode, I tried re-assigning the references (as in dragging the Slider and Text objects in their respective reference boxes) and it works exactly how I want it to after.

Does this have to do with how I called GetComponent for the UI elements or the fact that they're children of the Canvas object? Any help (even if it's just general code fixes) would be appreciated!

screen-shot-2016-12-30-at-120543-pm.png (39.3 kB)
screen-shot-2016-12-30-at-120555-pm.png (37.6 kB)
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
2
Best Answer

Answer by AurimasBlazulionis · Dec 31, 2016 at 04:49 AM

You try to get progress slider and text on awake. You do not need that. This is the reason you get these removed. Remove the awake method.

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 anmre21 · Dec 31, 2016 at 08:51 PM 0
Share

Thanks so much, you're a lifesaver! This worked perfectly :)

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

118 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 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 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 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 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 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 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

NullReferenceException from within GetComponent 2 Answers

Changing script variable from another script doesn't change it in the original script? 0 Answers

Problem to read an array from another script but on the same object 1 Answer

OnTrigger function problem 2 Answers

NullReferenceException in Two Scripts 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