|
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
(comments are locked)
|
|
If you are getting a Null Exception at the line which reads: 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. 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 ...
Jun 01 '12 at 10:00 PM
Mickman
If you look at the book reference you provided, just before the line which reads: if (originalMaterial.mainTexture) there is a statement which reads originalMaterial = GetComponent(MeshRenderer).material It is that statement that assigns a value to originalMaterial. That statement isn't present in your code.
Jun 01 '12 at 10:22 PM
kolban
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.
Jun 01 '12 at 10:31 PM
kolban
OK, I GOT IT SORTED .. Really appreciate the HELP sorry i thought the big button stated "ASK A QUESTION " perhaps it needs updating .... to ASK A TECHNICAL QUESTION. Well done... carry on.. nothing to see here folks, move along
Jun 01 '12 at 10:53 PM
Mickman
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 ....
Jun 02 '12 at 12:05 AM
Mickman
(comments are locked)
|

not sure why my lines of code are not layed out correctly when i post ?
it says my problem is on this line if (originalMaterial.mainTexture){