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 zancar615 · Apr 05, 2019 at 07:05 PM · scripting problemuitext

NullReferenceException on Text element

I have a script which, among other things, accesses a UI text element to display a timer that is constantly counting down. When the player interacts with certain game objects the timer goes up. For reasons unknown to me when I try to access the setTimer function (what sets the text in the UI text element) from the OnTriggerEnter function (where i deal with what happens when the player hits an object) it says the timerText object is null, however when i access it from the Start function and the Update function it works properly. Not sure whats going on here. (note the rocket boost functionality is also not working but thats not what im struggling with right now).

Heres the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class gameController : MonoBehaviour
 {
     //Values that are applied to UI elements
     private float gameTime;
     private int score;
     //UI Text Elements
     public Text scoreText;
     public Text timerText;
     public Text welcomeText;
     //collectable prefabs
     public GameObject hourGlass_prefab;
     public GameObject money_prefab;
     public GameObject rocket_prefab;
     //values for rocket boost logic
     private bool isBoosted;
     private float boostTime;
     //variables for player movement
     Vector3 movement;
     public float distance = 20f;
     new Rigidbody rigidbody;
 
     // Start is called before the first frame update
     void Start()
     {
         //set welcome test and initialize variables
         welcomeText.text = "Welcome to Spaceship Arcade! \nCollect the rings to score points!";
         isBoosted = false;
         
         score = 0;
         setScore();
         setTime(20.0f, true);
         gameTime = 20;
         Destroy(welcomeText, 3.0f);
         rigidbody = GetComponent<Rigidbody>();
         
     }
 
 
     // Update is called once per frame
     void Update()
     {
         //move player in camera direction
         transform.position = transform.position + Camera.main.transform.forward * distance * Time.deltaTime;
         //count down timer with system time
         setTime(Time.deltaTime, false);
         //logic for ending rocket boost
         if (isBoosted)
         {
             if(gameTime - boostTime >= 10.0f)
             {
                 distance = distance - 10.0f;
                 isBoosted = false;
             }
         }
         
     }
 
    
     
     //checks what object is collided with and acts accordingly
     private void OnTriggerEnter(Collider other)
     {
         Debug.Log("Object entered trigger");
         if (other.gameObject.name.Contains("score_ring"))
         {
             Debug.Log("Score_ring collided");
             //spawns next score ring
             GameObject newRing = Instantiate(other.gameObject);
             //determines if a bonus collectable will be spawned and what type,
             int rand = Random.Range(0, 10);
             if(rand >= 8)
             {
                 GameObject hourglass = Instantiate(hourGlass_prefab);
 
                 hourglass.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-10.0f, 10.0f), other.gameObject.transform.position.z + Random.Range(20.0f, 35.0f));
 
             }
             if(rand <= 2)
             {
                 GameObject rocket = Instantiate(rocket_prefab);
                 rocket.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-10.0f, 10.0f), other.gameObject.transform.position.z + Random.Range(20.0f, 35.0f));
 
             }
             if(rand >2 && rand < 6)
             {
                 GameObject money = Instantiate(money_prefab);
                 money.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-10.0f, 10.0f), other.gameObject.transform.position.z + Random.Range(20.0f, 35.0f));
 
             }
             newRing.transform.position = new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y + Random.Range(-20.0f, 20.0f), other.gameObject.transform.position.z + Random.Range(40.0f, 70.0f));
             score += 10;
             setTime(5.0f, true);
             setScore();
             Destroy(other.gameObject);
         }
         //hourglass gives points and extends timer
         if (other.gameObject.name.Contains("hourglass"))
         {
             Debug.Log("Hourglass Collided");
           //  setTime(10.0f, true);
             score += 5;
             setScore();
             Destroy(other.gameObject);
         }
         //moneybag gives large point boost and nothing else
         if (other.gameObject.name.Contains("moneybag"))
         {
             Debug.Log("Moneybad Collided");
             score += 20;
             setScore();
             Destroy(other.gameObject);
         }
         //rocket gives movement speed boost
         if (other.gameObject.name.Contains("roc"))
         {
             Debug.Log("RocketCollided");
             distance = distance + 10.0f;
             isBoosted = true;
            // boostTime = gameTime;
             Destroy(other.gameObject);
         }
     }
     //sets score on UI
     public void setScore()
     {
         scoreText.text = "Score: " + score.ToString();
     }
     //sets game time on UI (if pos is true then adds an amount, else subtracts)
     public void setTime(float time, bool pos)
     {
         float temp = gameTime;
         if (pos)
         {
            gameTime = temp + time;
         }
         else
         {
             gameTime = temp - time;
         }
 
         if (timerText != null)
         {
             timerText.text = gameTime.ToString() + " seconds";
         }
         else
         {
             Debug.Log("helpful error message");
         }
     }
 
 }
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
0

Answer by Vollmondum · Apr 06, 2019 at 05:05 AM

timerText is a text component, not an object. Use timerText.gameObject

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

296 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Accessing UI text 0 Answers

How do I keep two UI text unity3d on the scene 2 Answers

UI Text created from C# Script 0 Answers

UI Text Not Updating 1 Answer

Changing the UI Text 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