- Home /
change material when ontrigger
hi, im very very new to this,and im not very good at programing.
the question is this:
i have my player and 3 different objects, i want to change the material of my player when i collide with them, for ex: when the player hits a sphere (lets say A) it should get the material from that sphere(the sphere A material)
this is the code i have written so far to detect the collision with the objects (im using c# btw)
void OnTriggerEnter(Collider other) {
if (other.gameObject.name == "BasicFire")
{
Destroy (other.gameObject);
}
else if (other.gameObject.name == "BasicWater")
{
Destroy (other.gameObject);
}
else if (other.gameObject.name == "BasicGrass")
{
Destroy (other.gameObject);
}
}
Answer by getyour411 · Apr 11, 2014 at 03:52 AM
Consider changing the if/else to use the || operator since the result for all three is the same
if (condition1 || condition2 || conditionX ) {
player.renderer.material = other.renderer.material;
Destroy(gameObject);
}
That's off the top of my head so fix any syntax issues
THAN$$anonymous$$S!! IT WOR$$anonymous$$ED!! btw i tried using ||, but i keep geting an error, so i tried if else and it worked that way, im gonna try again then