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 rkGio · Oct 05, 2013 at 02:39 AM · error-messageaccessing from any scriptanother script

GetComponent Trolling Me?

I am making a Game in unity for an University assignment, and so far I have been doing good enough. I have made quite a lot of research every time i have come against an obstacle, and thus far I have solved all of my previous issues. However, this time, I'm having an issue with something I have already found the answer for, and I can't seem to find the mistake anywhere, no matter how hard I try to do so.

I am trying to access a variable from a different script, a Boolean to be specific, and while I have read how to do this, I can't seem to make it work.

The two scripts (not the final code, just what I was using to test if I could use the variable or not) are as follow:

 #pragma strict
 var crosshair : Texture2D;
 var welcomeMessage : WelcomeMessage;
 
 function Awake(){
 
     welcomeMessage = GetComponent(WelcomeMessage);
 }
 
 function Start () {
     
     
 }
 
 function Update () {
 
     welcomeMessage.messageDone = true; 
 }
 
 function OnGUI(){
     GUI.Label (Rect (Screen.width/2-250, Screen.height/2-150, 512, 512), crosshair);
 }



 
     #pragma strict
     var firstMessage : Texture2D;
     var secondMessage : Texture2D;
     var thirdMessage : Texture2D;
     var fourthMessage : Texture2D;
     var fifthMessage : Texture2D;
     var sixthMessage : Texture2D;
     var seventhMessage : Texture2D;
     var messageLifeSpan : float = 1.0f;
     var messageTimer : float = 0.0f;
     public var messageDone  : boolean = false;
     
     
     
     function Start () {
     
     }
     
     function Update () {
     //SetUp Timer to keep track of time.
         messageTimer += Time.deltaTime;
     }
     
         
     function OnGUI (){
     //wait 1 second, then show Message No1 for 8 seconds
         if (messageTimer > 1 && messageTimer < 9)
             GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), firstMessage);
     //wait another 2 seconds, then show message No2 for 12 seconds.
         else if (messageTimer > 11 && messageTimer < 27)
             GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), secondMessage);
     //wait another second, then show message No3 for 12 seconds.
         if (messageTimer > 29 && messageTimer < 45)
             GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), thirdMessage);
     //wait another second, then show message No4 for 12 seconds.
         else if (messageTimer > 47 && messageTimer < 63)
             GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), fourthMessage);
     //wait another second, then show message No5 for 8 seconds.
         else if (messageTimer > 65 && messageTimer < 73)
             GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), fifthMessage);
     //wait another second, then show message No6 for 12 seconds.
         if (messageTimer > 75 && messageTimer < 91)
             GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), sixthMessage);
     //wait another second, then show message No7 for 12 seconds.
         else if (messageTimer > 93 && messageTimer < 109)
             GUI.Label (Rect (Screen.width/2-200, Screen.width/2-300, 512, 512), seventhMessage);
     }


The error I am getting is: NullReferenceException: Object reference not set to an instance of an object Crosshair.Update () (at Assets/Scripts/Game/All Difficulties/All Levels/Crosshair.js:17)

And I cant understand why, I am using the format the Script Reference uses. Can anyone help me please?, I really need to be able to use variable from different scripts with ease.

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
1
Best Answer

Answer by TrickyHandz · Oct 05, 2013 at 03:05 AM

Is the "welcomeMessage" script attached to the same GameObject as your first script?

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 rkGio · Oct 05, 2013 at 09:05 AM 0
Share

That was it!, Thank you!, you are a Hero.

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

16 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

Related Questions

triggering an event in another C# script 1 Answer

Access a Script from Another Scene? "Object Reference not set..." 0 Answers

Plugging a script into another script when gameobject is selecte 1 Answer

Get GameObject From Another Script 1 Answer

Access a Script from Another Scene? "Object Reference not set..." 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