The name 'color' does not exist in the current context

Hi
My code is exactly the same as Ricks, nothing fancy, for what ever reason it says the color property doesn’t exist in this context and the code wont run due to the error.

Hi,

Welcome to our community! :slight_smile:

NullReferenceException means that a reference (“link”) to an instance is missing. Is there a MeshRenderer component assigned to the same game object as ObjectHit?

1 Like

Thanks Nina greatly appreciate the help! There is a mesh renderer component in every wall segment and even the player, to be doubly sure I ensured that the ObjectHit code is a component in all the game objects instead of just the player and I am having the same issue. The walls and player box also have the custom material as a component.

1 Like

Please check the material. Does it have got a color property? (I think it translates to _Color or _MainColor in the shader.)

the shader is set to standard, if you hit edit next to the drop box it comes up with a list of properties, _Color is one and it is also similar to _EmissionColor

All my colors have it.

The problem is that Material.color refers to a very specific property in the shader program. If the colour property in your shader has got a different name, Material.color won’t work. In that case, you would have to call GetColor with the “correct” name.

Shaders are very complex, so don’t worry if my answer is confusing. They are not part of this course for a reason.


It might be that I misinterpreted the error message. Please hover your mouse over color in VS Code. Also hover your mouse over material. What information do you get on those two “words”?

material is a property of the MeshRenderer class. And color is a property of the Material class. If you do not get this information when hovering over these two properties, there might be something wrong with VS Code.

In that case, go to Edit > Preferences > External Tools (in Unity) and click “Regenerate project files”.

If the issue persists, please follow the instruction on this website and make sure all required extensions are installed: https://code.visualstudio.com/docs/other/unity

Make sure the .NET Framework 4.7.1 (Developer Pack) is installed. Here is the link: https://dotnet.microsoft.com/download/dotnet-framework/net471

Depending on your version of Unity, it might be that you will have to install the “Visual Studio Code Editor” package in Window > Package Manager in Unity.

I downloaded all of that and tried to regenerate the project files, i got the error cannot perform upm operation ‘visual studio code’ is invalid. i have all of the extentions, the developer pack etc.
Progress! i can see the pop up while running over the color after the = sign, it says the name color does not exist in this context, the issue is with the second color, the one after the equals sign in the code.
And all i had to do was add a capital C to color. OK nina thanks for the help!

Oh noes. I focussed so much on material.color (cause it was highlighted in the screenshot) that I completely forgot to check if there is another color in that line. Next time, please share your code as formatted text because reading screenshots is really difficult. I could have used the search function to look for “color” instead of relying on my reading skills. :wink:

The compiler should have underlined the wrong/unknown color. I hope VS Code works properly now and highlights the problems in your code. :slight_smile:

no worries Nina, yep that works. also I remember changing back and forth with capitals and not. i think maybe something wasnt installed right, i wasnt getting the error highlights before but now i am. so thanks again.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms