• 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 /
This question was closed Feb 14, 2015 at 06:54 PM by Graham-Dunnett for the following reason:

Duplicate Question

avatar image
0
Question by Digital-Phantom · Feb 14, 2015 at 06:24 PM · guitextnullreferenceexceptionscoreguitext

Why this Error when trying to add a score using the new GUI ?

OK so I'm trying to add a scoring system to my game and as such have been watching the new GUI tutorials. I thought I'd followed things to the letter but when I press play I get the following error -

NullReferenceException: Object reference not set to an instance of an object ScoreManager.Update () (at Assets/Scripts/ScoreManager.cs:25

my script is -

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour
 {
     public static int score;        // The player's score.
 
     Text text;                      // Reference to the Text component.
     
     
     void Awake ()
     {
         // Set up the reference.
         text = GetComponent <Text> ();
         
         // Reset the score.
         score = 0;
     }
     
     
     void Update ()
     {
         // Set the displayed text to be the word "Score" followed by the score value.
         text.text = "S c o r e : " + score;
     }
 }
 

I have my canvas, with a text component. The text component (ScoreText) has the above script attached to it as described in the tutorial.

Either I've missed something which I just can't see (even after multiple times watching) or I simply don't understand what the guys are trying to tell me.

Any suggestions plz ???

Comment
Comments Locked · 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 awest · Feb 14, 2015 at 06:31 PM 0
Share

It is saying the text variable isn't assigned. I assume the GetComponent is failing. Double check to make sure both the text component and this script are on the same object.

You can try making the text variable public, commenting out the GetComponent line, and assign it in the inspector to make sure.

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Restrict characters in GUI.TextField 4 Answers

How to make GUI Text Scale to different Resolutions? 3 Answers

How to find the origin point of a text line? 0 Answers

Scrolling Text 1 Answer

Making a script for all gameobject GUITEXT. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges