- Home /
How I can get an instace of a material, created in a project panel
I try to do this by myMaterial = Instantiate("MaterialName") and myMaterial = new MaterialName() But without any success.
I'm a novice and can't yet clear understand the poject panel paradigm. Can I access to objects in it? This objects, it`s classes or instances?
Answer by duck · Dec 14, 2009 at 04:19 PM
The project pane represents files on your computer, which have been placed in the 'Assets' folder of your project. These are files which are ready for use in your scene, but which may or may not have been used yet.
With normal use in Unity, you generally don't access these by name from script. Instead you drag references from your project view to various target destinations. You might drag a material directly on to an object in your scene (represented either in the scene view, or the hierarchy pane).
Or if you want to refer to a particular material in a script, you could create a public variable of type 'Material' in your script. This would expose the material variable for that script in the inspector, and you can then drag the material into that slot.
However, if you are placing a script on a GameObject which is visible in the scene, the chances are that object already has a material. You can refer to the material of the object on which the script is placed by using renderer.material like this:
renderer.material.color = Color.red;
Notice (as described in the docs) that accessing the object's material like this will actually create a temporary runtime clone of the material (thus making it unique to that object, even if it was initially shared by many objects in your scene). To affect all models which share a material use renderer.sharedMaterial like this instead:
renderer.sharedMaterial.color = Color.red;
hope this helps!
Thank you very much! I better understand it now.
Reading mans, i find a class "Resources" what type of objects it supports? Can i Load predefined materials with it, or only textures?
"Resources" is the class that you use to access assets which are stored in your Resources folder, in your Assets. I think it supports most kinds of assets. However, don't use the Resources folder for general purpose work - make sure you understand how to use drag-references and prefabs first, because this is how unity is designed to work. Items placed in Resources should only be there for special reasons, not just because you don't understand how to use Prefabs and dragged references!
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
Share material on Instantiate() 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Assetbundle materials? 1 Answer