- Home /
Change material of only one instance of a prefab
Hello,
i have a prefab of my alien enemy. During the game i spawn these aliens in waves like:
GameObject clone = Instantiate(enemy_prefab, Spawnpoints[i].transform.position, Spawnpoints[i].transform.rotation) as GameObject;
++enemyCnt;
clone.name = "Alien" + enemyCnt;
During the game i need to change the material of some aliens, but everytime i change one, the material of ALL aliens in the scene changes. I really don't know how to handle this since i try it for a very long time now. It is no problem to change the color of a single alien, i have just problems in changing the materials.
Thank you for your answers!
*edit:
The idea behind the game is, that the enemies are invisible. You have a gun with two modes - deadly and paint. It works to map the paint onto the aliens when the renderer is off. So when i hit an alien i want it to flicker so the player can see the shape of it. Therefore i made a glass-like material. The actual problem is, when i start the flicker method of an alien - all aliens are flickering. Here is the code i wrote:
In Update Method:
if(flickerSequence > 0)
{
flickerTime -= Time.deltaTime;
if(flickerTime <= 0)
{
if(flickerSequence % 2 == 0)
setMaterial(MATERIAL.INVISIBLE);
else
{
if(!DEBUG_MODE)
setMaterial(MATERIAL.NONE);
else
setMaterial(MATERIAL.DEBUG);
}
flickerTime = FLICKER_INTERVAL;
--flickerSequence;
}
}
When i want it to flicker i set flickerSequence to the value how often i want it to flicker. This is how the material is changed (or the visibility state):
public void setMaterial(MATERIAL type)
{
//Debug.Log("Switching alien material of " + name);
switch(type)
{
case MATERIAL.DEBUG:
renderedObject.renderer.material = debugMaterial;
renderedObject.renderer.enabled = true;
break;
case MATERIAL.INVISIBLE:
renderedObject.renderer.material = invisibleMaterial;
renderedObject.renderer.enabled = true;
break;
case MATERIAL.NONE:
renderedObject.renderer.enabled = false;
break;
}
actMaterial = type;
}
Answer by nidhoeggr09 · Jan 07, 2012 at 03:39 PM
[Solved] It was a error in the game logic -> see comments below!
How exactly are you sending the 'flicker' command? What tells the renderer that it should be flickering?
Oh man ... i got it :D I followed your advice and added Logs on as many positions possible. It was a error in my logic combined with bad luck while testing.
When an alien gets hit by the player i call the "gotHit" function which does general things and further calls "hitPaint" or "hitGun" where the actual life is decreased and further ai-things are done. The flicker call is in "gotHit". Now when an alien gets hit, i also call "gotHit" of all aliens which are nearby the alien which got hit to make them search for the player and help the alien which originally gots hit.
When i was debugging with the Log in the set$$anonymous$$aterial function it seems that i always tested when no other aliens where nearby. I just can't think of any other way why i don't got that.
Anyway, my problem is solved, i am very happy about that and you are my hero for the day! Thank you very much!!
Your answer

Follow this Question
Related Questions
Instantiating model gameobject that is not tied with original asset 0 Answers
Changing material at runtime messes up with prefab materials [Solved] 1 Answer
Material keeps unloading 0 Answers
Changing a material colour in a prefab loses the material ??? 0 Answers
Different prefabs with same mesh but different textures(materials). 1 Answer