- Home /
changing emissive color on particle material
hi guys, im stuck, like the question says, im trying to change the emission color on a particles material, based on distance, i get no errors, the inspector show the distance fine, but the color dosent change, heres the code
#pragma strict
var distance: float;
var myTrans:GameObject;
var targetTrans:GameObject;
var targets : GameObject[] ;//hold an array of target objects
function Start()
{//find all the game objects tagged as target
targets = GameObject.FindGameObjectsWithTag("shoot me");
//if no target set run function find target
if(targetTrans==null){
findTarget();
}
}
function findTarget(){
//randomly pick a target from the collected array
var newTarget : int = Random.Range( 0, targets.length );
//set the chosen randomly picked target as the curren target
targetTrans = targets[newTarget];
}
function FixedUpdate () {
distance = Vector3.Distance (myTrans.transform.position, targetTrans.transform.position);
if (distance >=250){
myTrans.particleSystem.renderer.material.SetColor ("_Emissive",Color.green);
}
if (distance <=249 && distance >=100){
myTrans.particleSystem.renderer.material.SetColor ("_Emissive",Color.yellow);
}
if (distance <=99){
myTrans.particleSystem.renderer.material.SetColor ("_Emissive",Color.red);
}
}
Are you sure the shader you are using in the material has an 'Emissive' color? None of the particle shaders do.
ok i didnt look at the script in the shader, untill now, im using the standard mobile.particle vertexlitblended shader,
i looked in the source and the property i want to effect would be this
_EmisColor ("Emissive Color", Color) = (.2,.2,.2,0)
so i changed my if statements to myTrans.particleSystem.renderer.material.SetColor ("_EmisColor",Color.red);
but still nothing
I just tried it with the vertexlitblended shader, and it worked fine for me:
pragma strict
var mat : $$anonymous$$aterial;
private var pr : ParticleSystemRenderer;
function Start() {
pr = particleSystem.renderer;
pr.material = mat;
pr.material.SetColor ("_EmisColor",Color.red);
}
function Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
pr.material.SetColor ("_EmisColor",Color.green);
}
ok so i set my emmisive to be white, i changed my start function and added your variables var distance: float; var myTrans:GameObject; var targetTrans:GameObject; var targets : GameObject[] ;//hold an array of target objects var mat : $$anonymous$$aterial; private var pr : ParticleSystemRenderer;
function Start() {
//find all the game objects tagged as target
targets = GameObject.FindGameObjectsWithTag("shoot me");
//if no target set run function find target
if(targetTrans==null){
findTarget();
}
pr = particleSystem.renderer;
pr.material = mat;
pr.material.SetColor ("_EmisColor",Color.red);
}
this should turn the emissive color on the assigned texture to red, but dosent and i get this error now
InvalidCastException: Cannot cast from source type to destination type. targetColor.Start () (at Assets/targetColor.js:19)
the line 19 would refer to the line of code pr = particleSystem.renderer; i also tried changing my if statements to match your code but had a lot of errors which slowed everything, i tried swapping the code to change the texture and not the color but same problem
perhaps its my objects not the code, perhaps im setting up wrong? let me explain my scene i have many space ships spawning, each space ship object has a particle component, which is a texture of a white target, as the space ship gets closer the target particle material needs to change color