• 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 Kiwi2703 · Feb 05, 2014 at 12:30 AM · javascriptfunctionstaticglobal

Static function and variables error

Hi there :) I'm making a script for my game that would allow me to control subtitles (text, duration) from another scripts. A private script worked fine, but when I tried to convert it to a static one, I've come to a set of problems. I managed to fix many of them, but there's one I can't seem to fix. This is my SubtitlesControl.js script:

 static var Subtitles : GameObject;
 static var Subs : GUIText;
 static var SubsOccupied : boolean;
 
 function Start(){
     SubtitlesControl.Subtitles = SubtitlesControl.GameObject.Find("SubtitlesObj");
     SubtitlesControl.Subs = SubtitlesControl.Subtitles.GetComponent("GUIText");
     SubtitlesControl.SubsOccupied = false;
 }
 
 static function SubsSay(SubsText,SubsDuration){
     SubtitlesControl.SubsOccupied = true;
     SubtitlesControl.Subs.text = SubtitlesControl.SubsText;
     yield WaitForSeconds(SubsDuration);
     SubtitlesControl.Subs.text = "";
     SubtitlesControl.SubsOccupied = false;
 }

However, whenever I try to call "SubtitlesControl.SubsSay("Blah blah blah",2);" from another script, it spams the debug with error messages: "NullReferenceException: Object reference not set to an instance of an object", evidently in 13th row of the script. Can someone help me with that please? Thank you! :)

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 getyour411 · Feb 05, 2014 at 12:30 AM 0
Share

I don't do JS so don't know for sure, but on 11 in parameter ( ) do you have to tell it what type like string SubsText, int SubsDuration

avatar image Kiwi2703 · Feb 05, 2014 at 12:34 AM 0
Share

Thanks for the tip getyour411, but unfortunately this didn't fix the problem. Same error message still appears.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Loius · Feb 05, 2014 at 01:10 AM

This line should throw a compile-time error:

 SubtitlesControl.Subtitles = SubtitlesControl.GameObject.Find("SubtitlesObj");

Regardless, you want GameObject.Find, not SC.GO.Find

Your null reference is specifically because the ".subs" object was not set.

And as fsxffhsfsfsfsf says, you also want to look at your usage of SubsText.

Comment
Add comment · Show 1 · 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 Kiwi2703 · Feb 05, 2014 at 01:22 AM 0
Share

Thank you, removing the SubtitlesControl before GameObject.Find helped :)

avatar image
0

Answer by ffxz7ff · Feb 05, 2014 at 12:59 AM

Ummm try changing

 SubtitlesControl.Subs.text = SubtitlesControl.SubsText;

to

 SubtitlesControl.Subs.text = SubsText;

I can't see where you defined SubtitlesControl.SubsText but that's not what you're passing.

Comment
Add comment · Show 1 · 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 Kiwi2703 · Feb 05, 2014 at 01:08 AM 0
Share

Tried this but unfortunately it didn't help :( Thanks for the tip though.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

variable = true from another script 2 Answers

Setting Scroll View Width GUILayout 1 Answer

How do you achieve variables and functions that are global between scenes? What is the BEST way? 1 Answer

How can I preserve static object/data between editor and play? 2 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