Search Unity

Blend skybox script

Discussion in 'Scripting' started by GineHeim, Apr 28, 2021.

  1. GineHeim

    GineHeim

    Joined:
    Nov 19, 2020
    Posts:
    10
    I'm trying to animate the skybox blend values but no luck so far, i'm having trouble trying to correctly access the shaders properties which in my case is _Blend but not sure if im using it correctly?

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Blendskybox : MonoBehaviour
    4. {
    5.     Renderer blend;
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.          blend = GetComponent<Renderer>().material.SetFloat("_Blend", 1.0f);
    10.  
    11.          blend.material.shader = Shader.Find("Blend");
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         float blend = Mathf.PingPong(Time.time, 1.0f);
    18.         blend.material.SetFloat("_Blend", blending);
    19.     }
    20. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    Line 9 obviously does not compile, as the .SetFloat() method returns a void.

    Break that line up as it is clearly a "hairy line of code" even if it did compile.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    One.
    Thing.
    Per.
    Line.
    Please.

    Also, generally do NOT use Shader.Find() because it is fragile and subject to silent asset stripping.

    Instead, make an instance of the material you want and drag it into a public Material field.
     
  3. GineHeim

    GineHeim

    Joined:
    Nov 19, 2020
    Posts:
    10
    ok i got it to work somehow