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 Mickman · Jun 01, 2012 at 09:21 PM · null exception

Newbie code Error Null Exception .. get_main.Texture

Newbie going nuts again .... over CODE

I have a simple cube with a script attached .. the script should look to see if a texture is attached to the cube... & there is. I can see it (not just a colour)

I keep getting the following error;

NullReferenceException UnityEngine.Material.get_mainTexture () (at C:/BuildAgent/work/300357e52574df36/Runtime/ExportGenerated/Editor/Graphics.cs:1126) Interactor.Start () (at Assets/_Bear Prefabs/Bear Scripts/Interactor.js:17)

here is my script

 private var originalMaterial : Material;  
 private var aoTexture : Texture;
 
 
     function Start(){
              // search scene for an object named MainCamera
              cam = GameObject.Find("Main Camera");
              if (originalMaterial.mainTexture){  //if texture exists..
              aoTexture = originalMaterial.mainTexture; // asigns the materials texture.
              print (name + ":  " + aoTexture.name); 
              }
     }
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 Mickman · Jun 01, 2012 at 08:45 PM 0
Share

not sure why my lines of code are not layed out correctly when i post ?

avatar image Mickman · Jun 01, 2012 at 09:04 PM 0
Share

it says my problem is on this line if (original$$anonymous$$aterial.mainTexture){

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by kolban · Jun 01, 2012 at 09:38 PM

If you are getting a Null Exception at the line which reads:

 if (originalMaterial.mainTexture)

then that means that the variable called "originalMaterial" was never assigned a value. Looking at your code that does indeed seem to be the case. Since "originalMaterial" is marked as private, it can't have been assigned at the Inspector level and since the code you are executing is running in "Start", then there is nothing previous that would ever have assigned a value to it. You can't access or attempt to access properties of a variable if that variable was never assigned. Perhaps you meant to test and see if originalMaterial was ever assigned in the first place?

eg.

 if (originalMaterial == null)


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 Mickman · Jun 01, 2012 at 10:00 PM 0
Share

wow thanks for the speedy reply.... I thinkI found the issue.. I have not named my material "aoTexture" & "original material" will look over it now ...

avatar image kolban · Jun 01, 2012 at 10:22 PM 0
Share

If you look at the book reference you provided, just before the line which reads:

if (original$$anonymous$$aterial.mainTexture)

there is a statement which reads

original$$anonymous$$aterial = GetComponent($$anonymous$$eshRenderer).material

It is that statement that assigns a value to original$$anonymous$$aterial. That statement isn't present in your code.

avatar image kolban · Jun 01, 2012 at 10:31 PM 0
Share

Sorry ... Unity Answers is for providing answers to technical and usage questions about using Unity and isn't for providing custom code to achieve particular tasks. If you are looking for assistance for someone to write some code for you, I'd suggest posting to the forum. Your question was on why do I get an exception at that line and the answer is because the variable was null.

avatar image Mickman · Jun 01, 2012 at 10:53 PM 0
Share

O$$anonymous$$, I GOT IT SORTED .. Really appreciate the HELP

sorry i thought the big button stated "AS$$anonymous$$ A QUESTION " perhaps it needs updating .... to AS$$anonymous$$ A TECHNICAL QUESTION.

Well done... carry on.. nothing to see here folks, move along

avatar image Mickman · Jun 02, 2012 at 12:05 AM 2
Share

Sorry about that .. thanx for your time .. it works fine now.. Now I can go over my code with a fine tooth comb to get to terms with it all. I'm surei ntime I too will look back upon such measly code thinking to myself .. what was I thinking ....

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I keep getting a null reference exception, can anyone help? 0 Answers

NullReferenceException: Object reference not set to an instance of an object 2 Answers

How do i make sure an Object is initialized before using it ? C# 2 Answers

Tree Creator Null Exception 0 Answers

Trouble accessing a class variable from another class in C#. 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