Material Instance cannot be changed :

Hi everybody !

I have a problem with using materials. I create a gameObject and want to make him bit transparent. When I change the material in the editor all is fine but when I try to do it from script it doesn’t change.

Here is my source :

ObjectOnDrag 	   = (GameObject)Instantiate(Prefab.gameObject, gameObject.transform.position, gameObject.transform.rotation);
			ObjectOnDrag.name  = Prefab.name + (number++).ToString();
			ObjectOnDrag.layer = CONF.NORMAL_LAYER;
			ObjectOnDrag.AddComponent(typeof(MeshCollider));
			
			// Make it Transparent
			
			foreach(Material mat in ObjectOnDrag.renderer.materials)
			{
				mat.color = new Color(mat.color.r,mat.color.g,mat.color.b,50);
			}

Does someone know if there is somthing wrong with materials and unity by scripting. Thanks !

If you want to change the transparency of an object, change the alpha value of the color. It is a value between 1 and 0. A little bit transparent would be something like below:

mat.color.a = 0.8;

Thanks !
Using C# I cannot have access to the a attributes, but the problem was about alpha value which is as you were saying a float between 0 1 instead of the value between 0 255 like in the editor.