I found the following sample source on YouTube, and when I tested it, it worked well.
public class AlphaChange : MonoBehaviour
{
[SerializeField] private Material myMaterial;
private void Start() {
Color color = myMaterial.color;
color.a = 0.5f;
myMaterial.color = color;
}
}
I added the source to my project.
The script is as follows:
public GameObject umbrella;
Material umbrella_Material;
Color umbrella_alpha_color;
private void Start() {
umbrella_Material = umbrella.GetComponent<Material>();
umbrella_alpha_color = umbrella_Material.color;
}
My project is as follows.
GameObject: Umbrella has the following materials.
There is no problem when compiling, but the following error occurs when playing the game.
MissingComponentException: There is no 'Material' attached to the "Umbrella" game object, but a script is trying to access it.
You probably need to add a Material to the game object "Umbrella". Or your script needs to check if the component is attached before using it.
UnityEngine.Material.get_color () (at <d8d38b92d94a4ff8a91e61d39c6d19cd>:0)
SkillManager.Start () (at Assets/Scripts/Manager/SkillManager.cs:51)