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 adi5402 · Oct 01, 2016 at 06:57 AM · c #

Null Refrence Expection

I am pretty new to unity

I am trying to change the value of text object in the canvas on update to do this I attached a script called textEdit to my text object but it attached itself to the main camera also and throws and nullRefrenceExpection error pointing at the camera what am I doing wrong here.

my textEdit Script looks like this

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

public class textEdit : MonoBehaviour {

 public static int score;

 Text text;
 void awake()
 {
         text = GetComponent<Text>();

     score = 0;
 }
 // Use this for initialization
 void Start () {
    
 }
 
 // Update is called once per frame
 void Update () {
     text = GetComponent<Text>();
     text.text = "test";

 }

}

Comment
Add comment · Show 2
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 Vyra · Oct 01, 2016 at 09:49 AM 0
Share

As a side note: You probably don't want to update your text every frame (That's roughly 60 times per second). I'm guessing you want to display your score right? So ins$$anonymous$$d make the score variable non-static and make a non-static AddScore function ins$$anonymous$$d that increments your score variable and updates the text at the same time, and is called whenever the condition to gain points is met.

Statics shouldn't be used on in-game object instances, statics are used when there's only one instance of this variable, function or class.

avatar image adi5402 Vyra · Oct 02, 2016 at 12:42 PM 0
Share

This totally makes sense. Thanks @vyra but for now I need it to print on update on every frame as I am trying to capture some input from a bluetooth screen port and print the value to screen

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JedBeryll · Oct 01, 2016 at 07:06 AM

There is probably no Text component in the camera.

 void Update () {
      if (text != null) {
           text.text = "test";
      }
 }
Comment
Add comment · Show 2 · 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 adi5402 · Oct 01, 2016 at 09:47 AM 0
Share

Thanks man the error also makes sense now

pickahome

avatar image Owen-Reynolds · Oct 01, 2016 at 10:28 PM 0
Share

But lots of other problems. This person needs to work through simpler examples and more basics.

awake is misspelled. text is needlessly cached AND looked-up every frame. Why aren't they able to remove the script from the camera? static score ... ugg.

avatar image
0

Answer by adi5402 · Oct 02, 2016 at 12:40 PM

Thanks @Owen-Reynolds for you input . I am actually trying to learn how to capture serial bluetooth input so I want to print the value coming from the com ports to the screen while the game runs so I can then trigger and event based on the data I am getting. The bluetooth device I am trying to connect to you sends the speed of a car or cycle based on wheel rotation per second.

for now I was just trying to find a way to print to screen on update.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I make my character move left or right when I touch and hold my android device and go back to center when I release the touch? 0 Answers

C # script taking values from one object and modifying another. 0 Answers

click on object then zoom in 0 Answers

changing player position after scene load 0 Answers

Trigger Script not working 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