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

changing opacity of object with raycast

Discussion in 'Scripting' started by ChuckieGreen, Jan 28, 2022.

  1. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    I am trying to change the opacity of an object using a raycast, but it doesnt seem to change. I have no errors and the raycast debug.log is printing corrects, just the opacity is not changing. Could anyone please help with out with what might be the issue?
    Code (CSharp):
    1.  if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    2.         {
    3.             if (hit.collider.tag == "stationary")
    4.             {
    5.                 Debug.Log("Building hit");
    6.                 hitObject = hit.transform.gameObject;
    7.                 Color changeObjOp= hitObject.GetComponent<Renderer>().material.color ;
    8.                 changeObjOp.a = 0;
    9.             }
    10.             else if (hit.collider.tag == "player")
    11.             {
    12.                 Debug.Log("Player hit");
    13.             }
    14.             else
    15.             {
    16.                 Debug.Log("Else hit");
    17.             }
    18.         }
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Does it work when you set the alpha to zero in the inspector ? i think you have to switch to transparent (or fade) mode to use the alpha property...
     
  3. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352
    When i click on the color next to Albedo in the material shader and lower the a in there, the object changes opacity. I have the rendering mode set to transparent, is that what you mean?
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You get the colour and change the a value, but never assign it back to the material. Color is a value type, like int or Vector3.

    You also might need to set the colour differently depending on your shader and pipeline. Certain properties on the material class don't function properly outside of the built-in pipeline with the old standard shader.
     
  5. ChuckieGreen

    ChuckieGreen

    Joined:
    Sep 18, 2017
    Posts:
    352

    Ohhh of course, silly of me that that did not even occur to me. Thanks for pointing that out, I will see if i can sort it out now. I am just using the standard shader. Think i might be able to use setColor, so will try that