Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by SassMaster · Oct 04, 2014 at 03:49 PM · parameters

Error when passing string for message function?

I've been trying to add a little GUI element in the corner of the screen where I could display messages to the player telling him that he's been spotted, or that one of his crew have completed the objective you've set them. I assumed it'd be an easy kind of inherit a class and then use a Message function in that class to change the string from a different script, yet, I'm getting this error:

NullReferenceException MessageSystem.Message (System.String m) (at Assets/Scripts/MessageSystem.cs:18) DoorOpening.Update () (at Assets/Scripts/DoorOpening.cs:10)

Here's both of the scripts that are throwing the exception:

The Message method.

 GameObject line1;
     GUIText line1Text;
 
     void Update()
     {
         line1 = GameObject.Find ("Line1");
         line1Text = line1.GetComponent<GUIText>();
     }
 
     public void Message(string m = "")
     {
         line1Text.text = m;
     }


The method where I call Message.

 public class DoorOpening : MessageSystem {
 
     void Update()
     {
         if(Input.GetKeyDown("m"))
         {
             Message("Locked.");
         }
     }
 }
 

I really have no idea why the reference is null. I'm providing a reference to the method in the Update of the DoorOpening script.

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 robertbu · Oct 04, 2014 at 03:54 PM 0
Share

Unless the your GUIText object can be destroyed, there is no reason to do the Find() and GetComponent() in Update(). Do it in Start(). As for your problem:

  • $$anonymous$$ake absolutely sure that the game object with the GUIText is named (not tagged) 'Line1' exactly. Not 'line1' or 'Line 1' or '1ine1'.

  • $$anonymous$$ake absolutely sure that there is a GUIText component on the 'Line1' named game object.

  • $$anonymous$$ake sure there are not multiple 'Line1'-named game objects in the scene.

avatar image SassMaster · Oct 04, 2014 at 04:32 PM 0
Share

I just checked. It seems that every single one of those is true. I've got the GUIText component on my Line1 object.

Line1 is named "Line1" exactly, to the letter.

There is only one Line1 in the whole project.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by 767_2 · Oct 04, 2014 at 03:53 PM

check this

 public class DoorOpening : MessageSystem {
 
     void Start()
     {
      Message("");
     
     }
 
     void Update()
     {
         if(Input.GetKeyDown("m"))
         {
             Message("Locked.");
         }
     }
 }
     

Comment
Add comment · Show 3 · 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 SassMaster · Oct 04, 2014 at 04:29 PM 0
Share

Already did that. It didn't work either.

avatar image 767_2 · Oct 04, 2014 at 04:41 PM 0
Share

check the edited one

avatar image SassMaster · Oct 05, 2014 at 05:14 PM 0
Share

Nope. Still broken.

avatar image
0

Answer by crodriguez · Oct 05, 2014 at 08:42 PM

You cannot assign something to a variable you're going to pass in a function parameter. To be more clear, you cannot do this:

 public void Message(string m = "")

Instead, just insert this:

 public void Message(string m)
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Speed won't work as a parameter 1 Answer

Mecanim and trigger code 1 Answer

how to make function parameters don't need to declare (custom parameters)?? 1 Answer

Modify a material at runtime 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