How can this be fixed

I get the following error in MouseLook.cs. I’m new to C# so this is puzzling me

Error CS0120 An object reference is required for the non-static field, method, or property ‘Renderer.materials’ AncientTimes.CSharp

This is the current code.

if ( GetComponent()
){
GetComponent();
Renderer.materials[materialIndex].SetTextureOffset(textureName, uvOffset);
}
}
}

replying to myself since it wasn't possible to reply to the last comment. not sure about overloading the built-in classes although it should be possible - the 'not sure' is because you could just make your own anyway (to avoid confusion/problems) it should be possible to initialize them if you write the constructors too... you're right with it not really being a unity specific thing...did you try asking over at stackoverflow?

3 Answers

3

Your first GetComponent has no Generic Parameter (GetComponent()). Your if statement must evaluate to either true or false.

You’re trying to access Renderer.materials instead of renderer.materials. The class Renderer does not have a static field or property called materials, only instances of the class do. If this script is a MonoBehaviour, just change the case. If not, then access the scriptVar.renderer.materials property, where scriptVar is a reference to a MonoBehaviour.

var renderer = GetComponent();
if (renderer)
renderer.materials[materialIndex].SetTextureOffset(textureName, uvOffset);