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;

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 + ":  " +; 

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?


if (originalMaterial == null)