Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Why is this code not changing my material's alpha?

Discussion in 'Scripting' started by TheMariqua, Jun 17, 2023.

  1. TheMariqua

    TheMariqua

    Joined:
    Jun 17, 2023
    Posts:
    3
    I have this very simple script here.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Epilepsy: MonoBehaviour
    7. {
    8.     private Color colore;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         colore = this.GetComponent<Renderer>().material.color;
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         colore.a = Random.value;
    19.     }
    20. }
    21.  
    It should, on every frame, modify the object's Alpha every tick. (object is a red cube)
    Unfortunately, the object's transparency stays the same, and I have no idea of what's causing it.



    I've also tried changing the the shader of the material, from standard, to every type of shader that had transparent in its name, but it doesn't seem to be working. Why?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Is the material itself set to anything other than Opaque? Opaque will ignore alpha.

    upload_2023-6-17_21-19-9.png
     
  3. TheMariqua

    TheMariqua

    Joined:
    Jun 17, 2023
    Posts:
    3
    Yes! I've read about that, unfortunately, it's set as transparent (which means the rendering mode isn't the issue... maybe?)



    Tried changing the source as well, but it still didn't solve the issue.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    Oh ... I had a first real look at your code just now ... the issue is obvious:
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         colore = this.GetComponent<Renderer>().material.color;
    4.     }
    5.     // Update is called once per frame
    6.     void Update()
    7.     {
    8.         colore.a = Random.value;
    9.     }
    You change the local instance of color. Color is a struct, a value type, that means every assignment creates a copy. You actually need to assign it back after changing it, for example:

    Code (CSharp):
    1. var renderer = this.GetComponent<Renderer>();
    2. colore = renderer.material.color;
    3. colore.a = Random.value;
    4. renderer.material.color = colore;
     
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Are you instantiating these cubes from prefabs? Can you show how you're doing this?
     
  6. TheMariqua

    TheMariqua

    Joined:
    Jun 17, 2023
    Posts:
    3
    Yep! This was it.
    I was operating on the assumption that Color was an object,
    Thank you for your time!