I'll make this short. I have a gameobject and it has a sprite renderer and a particle renderer. What I want to do is, when I give it a command from the script, i want the sprite renderer of the gameobject to change the material, however if I do it like this : using UnityEngine; using System.Collections;
public class DoorBehav : MonoBehaviour {
public static float DoorHp = 100f;
public TextMesh HpText;
public Material brokenMat; //the material I want to use for the sprite renderer
private GameObject hero;
ParticleEmitter partEmit;
void Start () {
hero = GameObject.Find("Hero");
partEmit = GetComponent<ParticleEmitter> ();
HpText = transform.FindChild ("DoorHp").GetComponent<TextMesh>() as TextMesh;
HpText.color = Color.green;
}
void Update () {
if (Vector3.Distance (hero.transform.position, transform.position) < 1.8f)
{
HeroBehaviour.agent.speed = 0;
DoorHp-=1f;
partEmit.Emit();
HpText.text = ((Mathf.FloorToInt(DoorHp)).ToString()+"%");
if(DoorHp <=60f)
{
transform.renderer.material = brokenMat;
HpText.color = Color.yellow;
}
if(DoorHp <=30f)
{
HpText.color = Color.red;
}
if(DoorHp <=0)
{
//play sound : destroy door
HeroBehaviour.agent.speed = HeroBehaviour.moveSpeed;
Destroy(this.gameObject);
}
}
}
...
Basically, this is a door and when the hero gets close to it, it loses hp (hitpoints). If the hp variable is lower than 60, it changes the look of the door to a broken door. However, when I do this, it also changes the material of the particle renderer and it starts spilling out tiny little broken doors instead of the derbis of the broken door. How do I get it to ONLY change the material of the sprite renderer? I've tried countless times but I can't get it to work :( By the way, I can not add any children to this gameobject, it would mess up many things. Thanks in advance! :)
I found a fix myself. Basically, what I did was, I added a ParticleRenderer and got components from the gameobject just like I did with the particle emitter. Then I imported a material just for the particle renderer and after changing the sprite renderer's material, I also changed the particle renderer's:
using UnityEngine;
using System.Collections;
public class DoorBehav : MonoBehaviour {
public static float DoorHp = 100f;
public TextMesh HpText;
public Material brokenMat;
public Material particleMat;//the material for the particle renderer
private GameObject hero;
ParticleEmitter partEmit;
ParticleRenderer partRend;// the new particle renderer
void Start () {
hero = GameObject.Find("Hero");
partEmit = GetComponent<ParticleEmitter> ();
partRend = GetComponent<ParticleRenderer>();//importing the particle renderer
HpText = transform.FindChild ("DoorHp").GetComponent<TextMesh>() as TextMesh;
HpText.color = Color.green;
}
void Update () {
if (Vector3.Distance (hero.transform.position, transform.position) < 1.8f)
{
HeroBehaviour.agent.speed = 0;
DoorHp-=1f;
partEmit.Emit();
HpText.text = ((Mathf.FloorToInt(DoorHp)).ToString()+"%");
if(DoorHp <=60f)
{
transform.renderer.material = brokenMat;
partRend.material = particleMat;//changing the particle renderer's material
HpText.color = Color.yellow;
}
if(DoorHp <=30f)
{
HpText.color = Color.red;
}
if(DoorHp <=0)
{
//play sound : destroy door
HeroBehaviour.agent.speed = HeroBehaviour.moveSpeed;
Destroy(this.gameObject);
}
}
}
}