RTS Change building mesh when damaged.

Hi all, I have been researching how to change the mesh when one of the buildings is damaged. I have added the input area in the script to add the alternate meshes for medium and heavy damage. The part I am having trouble with now is actually getting it to work in game as new to coding in c# and not sure where to go. Here is what I have so far be it very simple.

    public mesh medium_damage;
    public mesh heavy_damage;

    void Update () {
	    if (health <= 0){
	        //Destroy building, reset tiles and so on
		if (gameObject.layer == 12) manager.temp.removeBuilding (this, false);
		if (gameObject.layer == 13){
			AImanager.temp.removeBuilding (gameObject);
		}
		DestroyUnit();
	}
    if (health <= 50) {
        
	}
}

Any help would be greatly appreciated Thanks.

Charlie.

Hi for me the easiest way is to create an empty GameObject with a collider box and put inside your three meshes (active the normal one and desactive the two other) after you can aplly this script

     public GameObject no_damage;
     public GameObject medium_damage;
     public GameObject heavy_damage;

     public int        health = 100;

     void Update () {
     if (health <= 50) {
         no_damage.SetActive(false);
         medium_damage.SetActive(true);
     }
     if (health <= 25){ 
         no_damage.SetActive(false);
         medium_damage.SetActive(false);
         heavy_damage.SetActive(true);
     }
     if (health <= 0)
         Destroy(gameObject);
 }

 void OnCollisionEnter(Collision collision) {
     if (collision.name == "bullet") { // or what ever you want
          Destroy(coliision.gameObject);
          health -= 10; // or what yo want
     }  
 }

You can assign the three object in the inspector.

I hope this help

Hi all, So after a trial and error session I found a part solutions.

public Mesh medium;
public Mesh low;

if (health <= 0) {
    	//Destroy building, reset tiles and so on
    	if (gameObject.layer == 12)
    	        manager.temp.removeBuilding (this, false);
    	if (gameObject.layer == 13) {
    	        AImanager.temp.removeBuilding (gameObject);
    	}
    	DestroyUnit ();
    } else if (health < 75) {
    	GetComponent<MeshFilter> ().mesh = medium;
    } else if (health < 25) {
    	GetComponent<MeshFilter>().mesh = heavy;
    }

The only problem now is if I place the building manually on to my map and run the mesh change works, however if I deploy the building (Construction Yard) the different damage meshes don’t load but the health works and tries to switch when building health gets to the desired level.

Any ideas why this works with manual placement but not with in game placement?

Thanks.

entire class, sorry still new to coding and what not lol.
did you mean the building script file?

Having trouble uploading the file as it exceeds the max characters… do u have an email i can send the file to?