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 Firgof · May 20, 2013 at 08:21 AM · gameobjectnullreferenceexceptioninstancingthis

NullReferenceException while trying to access a Component

I've been at this for hours, now, and I don't feel like I'm getting closer to understanding the underlying problem here. Running this script gives me: "NullReferenceException: Object reference not set to an instance of an object" on lines 31 and 15.

What am I doing wrong here?

Update: I've tried moving the alpha stuff out of that function but, unfortunately, the error persists. Updated the code example to what I've currently got. Also updated the error above.

Update 2: Related to this issue, I believe, is another script I've created wherein I both can print the instance of an object into the console and, simultaneously, Unity returns a NullReferenceException. In the example below, thisLight is apparently null but correctly prints into the console. My head.

Update 3: Have tried everything I could think of, can't figure out why it's throwing this error. Does anyone have the knowledge to pinpoint where this is going wrong?

Code from Update2

 thisLight = GameObject.Find("BrightCone");
 print(GameObject.Find("BrightCone"));

Original Problem's Code

 var hasTriggered: boolean = false;
 var hasFaded: boolean = false;
 var msg: String = "";
 
 var tempAlpha: float = 0.0f;
 function Start(){
     
     guiText.font.material.color.a = 0;
     guiText.text = msg;
 }
 
 function Update(){
     if(hasTriggered){
         if(!hasFaded){
             FadeIn();
         }
         else{
             tempAlpha = guiText.text.material.color.a + Time.deltaTime;
         }
     }
 }
 
 function OnTriggerEnter(c:Collider){
    if(c.gameObject.tag == "Player"){
         hasTriggered = true;
    }
 }
 
 function FadeIn(){
     if (hasFaded == false){
         guiText.text.material.color.a = tempAlpha;
     }
     else {hasFaded = true;}
 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by xandermacleod · May 20, 2013 at 04:01 PM

If you are using C# you may need to use a temporary variable instead of the += operator.

i.e.

 float tempAlpha;
 tempAlpha = gameObject.guiText.text.material.color.a + Time.deltaTime;
 gameObject.guiText.text.material.color.a = tempAlpha;
Comment
Add comment · Show 4 · 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 Firgof · May 20, 2013 at 04:13 PM 0
Share

Unfortunately, it's JS but I appreciate the reply. :(

avatar image Ashkan_gc · May 21, 2013 at 05:53 AM 0
Share

In C# you can not use color.a and set it, you should set color completely due to being a value type

avatar image Firgof · May 21, 2013 at 06:06 AM 0
Share

But is that relevant to JS? If not, though it's a valuable tip, I'm not sure how it applies directly to my problem above since I'm not using C# in my script?

Oh, unless I misread you and you're replying to xander?

avatar image Ashkan_gc · May 21, 2013 at 11:52 AM 0
Share

@Firgof yes i replied to xandermacleod

avatar image
0

Answer by Graham-Dunnett · May 20, 2013 at 10:42 AM

Delete gameObject. from your 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 Firgof · May 20, 2013 at 03:40 PM 0
Share

I thought I tried that before but guiText demanded that I give it an instance of a game object to attach to. I removed it per your instructions anyway, however:

Regardless of whether gameObject exists or not I still get NullReferenceExceptions on line 28. Specifically: NullReferenceException: Object reference not set to an instance of an object

avatar image
0

Answer by Ashkan_gc · May 20, 2013 at 06:34 PM

text property of the GUIText might be null

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 Firgof · May 20, 2013 at 06:47 PM 0
Share

Doesn't appear to be.


GUIText [It is enabled, in case it matters]

Text: WD-404

Anchor: middle center

Alignment: center

etc ...

Display Fading Text <-- this script

Script: displayFadingText

Has Triggered: N

Has Faded: N

$$anonymous$$sg: WD-404

avatar image
0

Answer by Loius · May 30, 2013 at 03:47 PM

Re: error on line 31:

GUIText: http://docs.unity3d.com/Documentation/ScriptReference/GUIText.html

.text is a string, it has no material

you want either guiText.material (more likely) or guiText.font.material

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 Firgof · May 30, 2013 at 05:40 PM 0
Share

Aha! That may be the source of that particular problem, aye. I'll check that out later today.

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

17 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

Related Questions

Why does my bullet prefab get destroyed? 1 Answer

Get game component type? 1 Answer

Can't add GameObjects to ArrayList 1 Answer

Destroy object last on application close. 1 Answer

Can't find gameobjects being spawned getting a nullreferenceexception 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