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 zero3growlithe · Apr 07, 2012 at 01:20 PM · errormaterialchange

Errors while assigning material to other object's by scripting.

I wanted to make a Start Race Counter thing in Unity, it was supposed to be two rotating rings with changing light inside of them and counter which shows "3,2,1,GO" and from my view it should work as it is now... but it does not and I have no idea why becouse it seem to be written good... I get this error although everything is assigned in Inspector as it should be:

NullReferenceException: Object reference not set to an instance of an object "line ~40"

Big thanks in advance :)

 var start : boolean = false;
 private var TurnLightOnOff : boolean = false;
 private var Counter : float = 0;
 private var Timer : float = 0;
 private var TimerFull : float = 0;
 
 var GO : Material;
 var Num1 : Material;
 var Num2 : Material;
 var Num3 : Material;
 var GreenLight : Material;
 var PurpleLight : Material;
 
 var OuterRing : GameObject;
 var InsideRing : GameObject;
 var Number : GameObject;
 var BckLight : GameObject;
 
 function Update () {
     OuterRing.transform.Rotate (Vector3(-2,0,0));
     InsideRing.transform.Rotate (Vector3(2,0,0));
     if (start == true){
         Timer += Time.deltaTime;
         TimerFull += Time.deltaTime;
         if (TimerFull >= 2){
             if (Timer >= 1){
                 Counter ++;
                 Timer = 0;
             }
         }
     }
     switch (Counter){
             case (1):
                 Number.renderer.material = Num3;
                 TurnLightOnOff = true;
                 return;
             case (2):
                 Number.renderer.material = Num2;
                 TurnLightOnOff = true;
                 return;
             case (3):
                 Number.renderer.material = Num1;
                 TurnLightOnOff = true;
                 return;
             case (4):
                 Number.renderer.material = GO;
                 BckLight.renderer.material = GreenLight;
                 GameObject.Find("GameController").GetComponent(RaceSequencer).StartTimerEnd = true;
                 return;
         }
         
     if (TurnLightOnOff && TimerFull < 5){
         BckLight.renderer.material = GreenLight;
         if (Timer >= 0.5){
             BckLight.renderer.material = PurpleLight;
             TurnLightOnOff = false;
         }
     }
     if (TimerFull >= 8){
         transform.Translate (Vector3(0,0,-1));
         Destroy (gameObject, 2);
     }
 }
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 GuyTidhar · Apr 07, 2012 at 05:51 PM 0
Share

To which line in the code you posted does the error take you too?

avatar image zero3growlithe · Apr 07, 2012 at 06:39 PM 0
Share

To where I'm changing an GameObject's material.

1 Reply

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

Answer by rutter · Apr 07, 2012 at 08:31 PM

A null reference exception usually means you're trying to access a reference which doesn't point to anything.

Take this line as an example:

 Number.renderer.material = Num2;

A null reference exception here could mean that Number is null, that its `renderer` property returned null, or that the renderer's `material` property returned null. You should check all three possibilities, in roughly that order.

Number seems to be a variable you're setting in the inspector. Make sure that you've done that.

Number.renderer will return null if Number doesn't have a renderer directly attached. Check that. Remember that you can also use `GetComponentInChildren()` if the renderer is not directly attached.

Number.renderer.material will return null if the renderer does not have a material set (usually in the inspector). Make sure that you've done that.

You should repeat these checks for every line that's erroring out as you've described.

Comment
Add comment · Show 7 · 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 zero3growlithe · Apr 08, 2012 at 02:50 PM 0
Share

But if I say that: Renderer is directly attached to object and material is assigned where it should be and number is assigned too? Is there any other possibility of what can be wrong?

avatar image rutter · Apr 09, 2012 at 04:42 PM 1
Share

Some reference has to be null at the particular moment you access it, or you wouldn't be getting that exception. We know empirically that something is wrong, and that it involves a null reference.

Only question left is: which reference, and why? Not necessarily an easy question, but it's good to know where we're at.

  • Open the editor's console window.

  • Run your game, reproduce the null reference exception.

  • Use the log output to identify the line of code that errors out.

  • Go to that line, identify all references on that line.

  • Immediately before that line, add some Debug.Log() statements to print out those refs.

  • Run your game, reproduce the null reference exception.

  • Check the log output again. See which variable was null, or which log statement errored out.

  • Using that extra knowledge, troubleshoot.

Assu$$anonymous$$g the problem line is the one I quoted above, your debug calls could look something like this:

 Debug.Log(Number);
 Debug.Log(Number.renderer);
 Debug.Log(Number.renderer.material);
 Debug.Log(Num2);
 Number.renderer.material = Num2;

If you're comfortable with it, it might be much faster to just use $$anonymous$$onoDevelop's built-in debugger, add a breakpoint at the problem line, and use that to check out the program's state just before the exception happens... that's more powerful, but also a steeper learning curve.

avatar image Kleptomaniac · Apr 09, 2012 at 04:57 PM 1
Share

Hang on a $$anonymous$$ute ... Number is a class, so na$$anonymous$$g it as a variable should cause a NullReferenceException ...

I'm not even sure what it's a class for though ... I've never seen it used before ...

EDIT

Aha! It's for doubles! I've really wanted to know what doubles were for a while though ...

There's your problem! Use something other than Number as your variable name.

avatar image rutter · Apr 09, 2012 at 05:46 PM 0
Share

Good catch!

avatar image Kleptomaniac · Apr 09, 2012 at 05:48 PM 0
Share

Haha, it was highly flukey but thankyou! :D

Show more comments

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

Changing GameObject texture? 4 Answers

just a simple question.. 1 Answer

Vertex Fragment Shaders are BROKEN? 2 Answers

Fix this script? 0 Answers

Expand script to stop camera from following player 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