Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by nidhoeggr09 · Jan 07, 2012 at 02:30 AM · prefabmaterial

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;
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Jan 08, 2012 at 09:39 AM 0
Share

Thanks for fixing your question.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by nidhoeggr09 · Jan 07, 2012 at 03:39 PM

[Solved] It was a error in the game logic -> see comments below!

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nidhoeggr09 · Jan 08, 2012 at 08:40 AM 0
Share

Does nobody has a solution for this? Please help me!

avatar image syclamoth · Jan 08, 2012 at 09:17 AM 0
Share

Are there any static variables in there?

avatar image nidhoeggr09 · Jan 08, 2012 at 09:21 AM 0
Share

no, nothing is static

avatar image syclamoth · Jan 08, 2012 at 09:25 AM 0
Share

How exactly are you sending the 'flicker' command? What tells the renderer that it should be flickering?

avatar image nidhoeggr09 · Jan 08, 2012 at 10:29 AM 1
Share

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!!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges