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
0
Question by dbrizov · Apr 15, 2014 at 09:57 AM · prefabmaterialruntimechanging

Changing material at runtime messes up with prefab materials [Solved]

Hi guys, I have a problem. I am making a Bubble Breaker game and so far I have made only the basic bubbles (red, green, blue, yellow, magenta). I have separate materials and prefabs for each bubble. Now I am trying to create a ChameleonBubble which changes it's color to one of the basic ones each turn.

Here is the code for adding the ChameleonBubble in the game scene:

 public void AddChameleonBubbleAtRandomPosition()
     {
         int row = UnityEngine.Random.Range(0, Rows);
         int column = UnityEngine.Random.Range(0, Columns);
         Destroy(this.bubbles[row, column].GameObject);
         this.bubbles[row, column] = null;
 
         GameObject randomNormalBubble = this.GetRandomNormalBubblePrefab();
 
         GameObject chameleonBubblePrefab = this.GetBubblePrefab(GameObjectName.ChameleonBubble);
         
         GameObject newChameleonBubbleGameObject =
             (GameObject) Instantiate(chameleonBubblePrefab, Vector3.zero, Quaternion.identity);

         // Here I change the color of the chameleon bubble
         // to a random one from the normalBubblePrefabs
         newChameleonBubbleGameObject.renderer.material = randomNormalBubble.renderer.material;
         newChameleonBubbleGameObject.transform.parent = this.transform;
         newChameleonBubbleGameObject.transform.localPosition = new Vector3(column, -row, 0);
 
         this.bubbles[row, column] =
             new ChameleonBubble(row, column, this.GetTypeOfBubble(randomNormalBubble), newChameleonBubbleGameObject);
     }

Everything works fine when I run the scene. The problem is that when I stop the scene, the materials of the prefabs of the normal bubbles are reset (In the MeshRenderer Component there is a message "Missing Material". That is only for the prefabs which materials have been used to create the new ChameleonBubble. For example (if in the scene a yellow ChameleonBubble appears, then only the material of the yellow normal bubble prefab is missing, the other normal bubble prefabs are ok). Do you know what the problem might be? I even tried "newChameleonBubbleGameObject.renderer.material = new Material(randomNormalBubble.renderer.material)" but that didn't work either.

Comment
Add comment
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

1 Reply

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

Answer by Santa · Apr 15, 2014 at 05:18 PM

You are accessing randomNormalBubble.renderer.material. And randomNormalBubble is prefab.

Acessing 'material' creates new material automatically and replaces it in prefab. Renderer-material

You should use randomNormalBubble.renderer.sharedMaterial instead.

Comment
Add comment · Show 3 · 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 dbrizov · Apr 15, 2014 at 06:36 PM 0
Share

Thank you so much. It worked. I didn't know that. Thanks again. I don't have enough points to vote you up, but I will when I get those points, you can count on it :)

avatar image Santa · Apr 15, 2014 at 06:38 PM 0
Share

You are welcome :)

avatar image MortePCAndVR · Jan 18, 2015 at 01:52 AM 0
Share

Thank You Santa!

This fixed an issue I only started having today, every other search finds this same issue with vague recommendations to use Inspector for prefabs (which I did use) and not use Resources.Load (which I did not use) and to copy prefabs by value rather than reference,

but only YOUR recommendation solved the issue caused by my usage of material rather than shared$$anonymous$$aterial, even though my usage was supposedly LHS read-only, that was enough to clear the assigned material from the prefab :)

I would also upvote this but I don't have 15 reputation yet...

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing the material on a prefab 0 Answers

Changing the material at runtime for multiple objects in a single call 0 Answers

transforming a prefab randomly at runtime 3 Answers

Overwriting a prefab in runtime? 1 Answer

Possible Unity Glitch. A few lines of code erasing prefab data. 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