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
0
Question by jmoriarty · Nov 01, 2015 at 11:29 PM · prefab

Roll a Ball game - NullReference Exception

I started with the Build a Ball tutorials, but am stuck on tutorial 7, "Displaying Score and Text". The program runs, but the score stays at 0 when the "Player" hits the "Pick Ups". The error that I am getting is NullReference Exception: Object reference not set to an instance of an object PlayerControl.Start() (at Assesst/Scripts/PlayerControl.cs: 21.

I check the program with the printout of the tutorial code and all of the variables. Does someone have a working game I could look at? Or if you had this problem, how did you fix it?

Comment
Add comment · Show 3
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 Statement · Nov 01, 2015 at 11:34 PM 0
Share

You probably forgot to set a field in the inspector of PlayerControl.

avatar image jmoriarty · Nov 02, 2015 at 12:57 PM 0
Share

I have the following objects in my Hierarchy:

$$anonymous$$ain Camera Directional Light Ground Player Walls - East, West, North, South Pick Ups - PickUp to PickUp4 Canvas - Count Text The error line is at 21 winText.text = "";

avatar image jmoriarty · Nov 02, 2015 at 02:01 PM 0
Share

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class PlayerControl : $$anonymous$$onoBehaviour {

 public float speed;
 public Text countText;
 public Text winText;

 private Rigidbody rb;
 private int count;
 
 
 void Start () 
 {
     
     rb = GetComponent<Rigidbody>();
     count = 0;
     SetCountText ();
     winText.text = "";
 }

 void FixedUpdate()
 
 {
     float moveHorizontal = Input.GetAxis ("Horizontal");
     float moveVerticle = Input.GetAxis ("Vertical");
     
     Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVerticle);
     
     rb.AddForce (movement * speed);
     
     
 }

 void OnTriggerEnter(Collider other)
 { 
     if (other.gameObject.CompareTag ("Pick Up"))

     {
         other.gameObject.SetActive (false);
         count = count + 1;
         SetCountText ();
     }

} void SetCountText () { countText.text = "Count: " + count.ToString (); if (count >= 8) { winText.text = "You Win!"; } } }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Nov 02, 2015 at 01:39 PM

 public Text winText; // Hello, I am null!!!

winText is null. you need to assign it in the inspector.

Alternatively, make the code work without winText assigned.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerControl : MonoBehaviour
 {
     public float speed;
     public Text countText;
     public Text winText;
 
     private Rigidbody rb;
     private int count;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         WarnMissingReference(countText, "Count Text");
         WarnMissingReference(winText, "Win Text");
         UpdateText();
     }
 
     void FixedUpdate()
     {
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVerticle = Input.GetAxis("Vertical");
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVerticle);
         rb.AddForce(movement * speed);
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject.CompareTag("Pick Up"))
         {
             other.gameObject.SetActive(false);
             ++count;
             UpdateText();
         }
     }
 
     void UpdateText()
     {
         bool win = count >= 8;
         SetText(winText, win ? "You Win!" : "");
         SetText(countText, "Count: " + count);
     }
 
     void SetText(Text obj, string text)
     {
         if (obj)
             obj.text = text;
     }
 
     void WarnMissingReference(Object obj, string name)
     {
         if (!obj)
             Debug.LogWarningFormat(this, "{0} has not been assigned", name);
     }
 }
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

34 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

Related Questions

The Unity version determines the hierarchical order of the Scenes 0 Answers

Prefab sprite glitch,Prefab canvas glitch 1 Answer

Prefab script member values not correct 0 Answers

Obstacle prefab falls through floor 1 Answer

UI changed in game affects prefab instances 0 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