Search Unity

Changing Material Shader Value in Run time.

Discussion in 'Scripting' started by ashishkushwaha, May 31, 2019.

  1. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    Hello All,

    Here is the enemy health script. When Enemy health gets zero, I want to change some values in material shader. So I changed the material shader value. The current enemy whose health is zero, script changes the value of material shader. But there is a problem, another enemy whose health is 100 also sharing the same material and their material value also gets changed which should changed when enemy health become zero. So, what should I do? And after facing this problem I came to know that if we change the value of material shader in run time, its never gets back to its default value. And its beyond my level.:(

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class EnemyHealth : Enemy, Idamageble
    9. {
    10.     //public new Animator anim;
    11.     // public Rigidbody enemyRigidBody;
    12.     public BoxCollider boxCollider;
    13.     public SphereCollider sphereCollider;
    14.     public NavMeshAgent navMeshAgent;
    15.     public GameObject money;
    16.     public Image healthBar;
    17.     private float startHealth;
    18.     AudioSource enemyAudio;
    19.     public GameObject enemyShadow;
    20.     public Gun_Controller mainGunScript;
    21.  
    22.     public Canvas healthBarObject;
    23.     public GameObject deathexplosion;
    24.    
    25.     public  Material  DisolveMaterial;
    26.  
    27.     float disolvevalue = 0.1f; // controlling disolve shader value
    28.     float DisolveSpeed = 0.3f; // refrence to disolve shader value speed
    29.    
    30.     void Awake()
    31.     {
    32.        // Renderer rend = GetComponent<Renderer>();
    33.         enemyAudio = GetComponent<AudioSource>();
    34.         // enemyShadow = GetComponentInChildren<GameObject>(); // enemy shadow
    35.         healthBarObject = GetComponentInChildren<Canvas>();
    36.         // = GetComponent<Renderer>().material;
    37.        
    38.      //DisolveMaterial =   GetComponentInChildren<Material>();
    39.          DisolveMaterial.SetFloat("_DissolveAmount", 0);
    40.     }
    41.  
    42.     private void Update()
    43.     {
    44.         disolveMethod();
    45.  
    46.     }
    47.  
    48.  
    49.  
    50.     public float Health { get; set; }
    51.  
    52.     public override void Init()
    53.     {
    54.         base.Init();
    55.         startHealth = base.health; // transfer Enemy class health value to startHealth
    56.         Health = startHealth;
    57.  
    58.     }
    59.  
    60.  
    61.     public void ActivateRugdoll()
    62.     {
    63.         anim.enabled = false;
    64.     }
    65.  
    66.     public void TakeDamage(float amount)
    67.     {
    68.         if (Health <= 0)
    69.         {
    70.             // StartCoroutine(giveVAlue());
    71.             //  disolveValueLoop();
    72.            // enemyMOV.NavMeshDisable();
    73.             return;
    74.  
    75.         }
    76.         else if (Health >= 0)
    77.         {
    78.             Health -= amount;
    79.  
    80.             StartCoroutine(EnableDisableHealthBar());
    81.             enemyAudio.Play();
    82.             Debug.Log("you can play animation of hit");
    83.             healthBar.fillAmount = Health / startHealth;
    84.  
    85.             StartCoroutine(Screaming());
    86.             if (Health <= 0f)
    87.             {
    88.                 enemyMOV.NavMeshDisable();
    89.                 Debug.Log(name + " Enemy DEAD");
    90.                 navMeshAgent.speed = 0f;
    91.  
    92.                 DestroyEnemyShadow();
    93.  
    94.                 ActivateRugdoll();
    95.  
    96.                 //DisolveMaterial.SetFloat("_DissolveAmount", disolvevalue + addValue );
    97.                 //StartCoroutine(giveVAlue()); // phele couroutine chailge
    98.                 disolveMethod();
    99.                 Destroy();
    100.             }
    101.             if (Health <= 50f)
    102.             {
    103.                 StartCoroutine(mainGunScript.AfterShootImage()); //  CRITICAL IMAGE ANIMATION
    104.  
    105.             }
    106.  
    107.         }
    108.  
    109.  
    110.     }
    111.  
    112.     private IEnumerator Screaming()
    113.     {
    114.         anim.SetBool("Scream", true);
    115.         enemyMOV.NavMeshDisable();
    116.         // Wait for 1 second
    117.         yield return new WaitForSeconds(0.5f);
    118.         anim.SetBool("Scream", false);
    119.         enemyMOV.NavMeshEnable();
    120.     }
    121.     void disableAllCollider()
    122.     {
    123.         boxCollider.enabled = false;
    124.         sphereCollider.enabled = false;
    125.  
    126.     }
    127.     void Destroy()
    128.     {
    129.         Destroy(this.gameObject,2.5f);
    130.         Instantiate(money, transform.position, Quaternion.identity);
    131.      
    132.     }
    133.     void DestroyEnemyShadow()
    134.     {
    135.         Destroy(enemyShadow);
    136.     }
    137.     // enable or disable health bar what shoot start or in technical when health decrease
    138.     IEnumerator EnableDisableHealthBar()
    139.     {
    140.         healthBarObject.enabled = true;
    141.         yield return new WaitForSeconds(0.4f);
    142.         healthBarObject.enabled = false;
    143.  
    144.     }
    145.     private void disolveMethod() // this method is create to make changes of value over time in dissolve shader ,
    146.     {
    147.  
    148.         if (Health <= 0)
    149.         {
    150.          
    151.            
    152.           DisolveMaterial.SetFloat("_DissolveAmount", disolvevalue += Time.deltaTime * DisolveSpeed);
    153.         }
    154.     }
    155. }
     
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    ashishkushwaha likes this.
  3. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    Thanks for the reply.:) But I am preparing code for mobile platform so extra drawcall will definitely the problem. And I'm still learning so the method you have suggested, I'm unable to use that in my script. Can you edit my script for me?
    Somewhere I have read in these kind of situations where multiple mashes share one material, people use some kind of material batch in run time. But I have no idea about that. That's why I'm asking. :(
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    679
    Take a look on both links that grizzly posted, they are the solution for your situation.
     
  5. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    oky ! yes its working thanks for the reply :)
     
    Last edited: Jun 1, 2019
  6. ashishkushwaha

    ashishkushwaha

    Joined:
    May 12, 2015
    Posts:
    35
    grizzly and brunocoimbra like this.