Assigning Materials[] produces populated array.

Issue

.

Defining an empty array of materials produces a populated array of newly instantiated materials that were never asked for.

.

Example:

Materials[] emptyMaterials = new Materials[10]; // <-- this is an empty array, 10 indexes long.
gameObject.GetComponent<MeshRenderer>().materials = emptyMaterials; // <-- this is now full

.

Apparently, materials isn’t a simple field, rather someone wrote it as a property (since adding more overhead to every call is always faster than no code… [ha ha]). Funny thing, the original emptyMaterials array is still empty. Assigning it didn’t actually update the reference at all. Unity simply loops over that and uses it as a suggestion, and then goes off to make an entirely new array on its own. That property loops through and fills it with entries when it finds a null.

.

I’m already trying to kill unnecessary new calls in my own code. I am going to populate that array of materials eventually. The fact that the getter/setter is automatically volunteering this behind my back is quite vexing.


Tangential

.

  1. I’d like to submit a bug report, but it looks like this will need to start as an inquiry (read as: unable to submit bug report through “Issue Tracker”).
  2. The formatting of these questions is horrendous. Notice the periods, and note that they are there because these forums strip returns so as to reduce readability by implying that we have no paragraphs.
  3. This site is missing a decent syntax parser while composing questions (though it exists in the final post).
  4. Selecting a set of return-separated sentences to become a list simply strips the sentences of their return characters, adds one bullet, and inserts new returns at a column width of 36 characters.
  5. List items do not indent properly. Actually… they’re running off the page to the left by 1em. Seriously, who left the site in this state?
  6. The “Markdown Basics” tooltip states “basic HTML tags are supported”, yet it’s not responding to basic html markdown such as: <font color="white">.</font>

Assigning an array to materials does on purpose create instances of the supplied materials. That’s the point of the materials property. What you may want to use is the sharedMaterials property which will actually use the supplied instances without creating a seperate material instances for this renderer.

Apart from this note that the Unity editor will always create a fake null object for serialized null values. However this behaviour only exists inside the editor and not in a build game. See this Unity blog post for more information.

Note that pretty much all of the build-in Unity classes / components have only properties and no actual fields. That’s because those components are actually written in C++ and the C# class is just a managed wrapper class. When you assign an array to materials (or sharedMaterials) Unity will not “save” that array internalls but just use the passed values and store them on the native side. Whenever you read the materials or sharedMaterials property you will actually get a new managed array that is filled with the internally stored references.

Finally we are aware of the broken markdown formatting. Unfortunately the markdown rendering broke about one and a half years ago and hasn’t changed since. For example my IMGUI crash course used to look like this:github clone. Basic HTML does work to some degree. Though to actually get a paragraph seperation you have to use the tag surrounded by empty lines. Note that the font tag is no longer valid in HTML5. Apart from that coloring of text is on most Q&A sites prohibited to avoid abuse. Your sample is the best example. Trying to use white text on white background would be constantly used by spammers to hide links in their posts.