Home > Net >  Change Material Range in only one object
Change Material Range in only one object

Time:07-11

I've created a dissolve Shader and wrote a script to run it. The point is that it changes the materials of all the objects. How could I make the Materials values change only for one object?

Here is the script:

public Material Mushroom;

private float currStrength;
private float maxStrength;
private float speed = 0.6f;
public bool let_dissolve = false;

public void onm ouse()
{
    Dissolve_rules_F();
    Invoke("Dissolve_rules_T", 0.3f);
}


public void Dissolve_rules_T()
{
    currStrength = 1.0f;
    maxStrength = 0.001f;
    let_dissolve = true;

}
public void Dissolve_rules_F()
{
    currStrength = 0.001f;
    maxStrength = 1.0f;
    let_dissolve = false;
}


public void Update()
{
    if (let_dissolve)
    {
        currStrength = Mathf.MoveTowards(currStrength, maxStrength, speed * Time.deltaTime);
        Mushroom.SetFloat("_Dissolve", currStrength);
    }

    if (!let_dissolve)
    {
        currStrength = Mathf.MoveTowards(currStrength, maxStrength, speed * Time.time);
        Mushroom.SetFloat("_Dissolve", currStrength);
    }
}

CodePudding user response:

It seems that you are making changes directly to your material, that is shared across different instances.

If you need to change only the concrete instance's material, you need to get it from the renderer like:

Material materialToApplyChanges = _targetObjectRenderer.material;

The material property will return the copy, not the shared instance.

But beware of making this too often. The more instances of the material present on the screen - the more draw calls you will get. It can cause lags.

  • Related