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 /
This question was closed May 12, 2017 at 03:04 AM by AlphafishGames for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by AlphafishGames · May 12, 2017 at 02:45 AM · scripting problemuitextnull reference exception

Null reference exception to a non-null text object?

Hi everyone--

I'm making my first non-tutorial game in Unity and I seem to be having an issue with setting text. I'm trying to make a score text object. Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

 public class EnemyDies : MonoBehaviour {
     public Rigidbody2D rb;
     public static int score;
     public Text text;
     // Use this for initialization
     void Awake () {
         text = GetComponent<Text> ();
     }
     
     // Update is called once per frame
     void Update () {
         string scores = score.ToString();
         text.text = scores;
     }
 
     public void OnTriggerEnter2D (Collider2D attack)
     {
         if (attack.gameObject.CompareTag ("Attack")) {
             if (transform.position.y <= 0.25F && transform.position.y >= -0.25F) {
                 score = score + 2;
             } else {
                 score = score + 1;
             }
             transform.position = new Vector3 (0, 6, 0);
             rb.velocity = new Vector2 (0, 0);
         }
         if (attack.gameObject.CompareTag ("Boundary")) {
             transform.position = new Vector3 (0, 6, 0);
             rb.velocity = new Vector2 (0, 0);
         }
     }
 }

Both text and rb are defined in the editor. I keep getting a null reference exception on the line text.text = scores, even though this is how all the tutorials I could find did this. Can anyone help?

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 Matthewj866 · May 12, 2017 at 02:47 AM 0
Share

@AlphafishGames in this case scores must be null. If the objects have been set in the inspector that's the only possible explanation.

What value does scores have when it gets assigned to text.text?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by AlphafishGames · May 12, 2017 at 03:04 AM

Okay, never mind. It turns out I was being stupid and I only needed to put text.text = score.ToString();. It must've been the GetComponent in the awake function that was messing me up or something. It's resolved now. :)

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

Follow this Question

Answers Answers and Comments

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

ui Text not responding to collider 1 Answer

UI Text: Words at end of line jumping to next line 0 Answers

How to render text on top of the objects that are generated on the map using a script? 1 Answer

problem with Adjustable Character Spacing script in Unity 5.4 0 Answers

NullReferenceException on Text element 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