Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mortim · Jan 21, 2010 at 04:29 PM · materialdestroyscene-loadingrenderer

Why does my materials get destroyed on some persistant objects ?

In my game, I have an object hierarchy whose root object calls the DontDestroyOnLoad function in its awake function. As a result, all the objects in this hierarchy are not destroyed when I load a new scene.

However, I have a mesh render object in this hierarchy which will lose all its materials when loading a new scene. The object is a sphere mesh with 5 materials and the object itself is not destroyed. The weird thing is that it has a child object which does not lose its material in the process.

I have solved the problem by adding the following code in its awake function :

function Awake() {
    for(m in renderer.materials)
        DontDestroyOnLoad(m);
}

However, I do not understand why my materials are not automatically preserved from destruction. I have many other objects which do not lose their materials at loading, without having to save them explicitly.

Can someone shed some light on this ?

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 Ashkan_gc · Jan 21, 2010 at 04:37 PM 0
Share

send your project folder to unity bug report or put it here as a link for others to test. i think it's a bug. if it isen't you might have some code on the object that do this.

2 Replies

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

Answer by Lucas Meijer 1 · Jan 21, 2010 at 11:06 PM

One thing that bites people sometimes is that if you do:

Material m = renderer.material;

we actually implicitely make a copy of the material, assign that to the renderer, and return that to you. (PS .sharedmaterial does not do this).

I could imagine this happening if you somehow access the .material property during initizliation of your object hierarchy. If that's not the case, please file a bug. (UnityMenu->Help->Report a problem)

Comment
Add comment · Show 1 · 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 Mortim · Jan 22, 2010 at 01:00 AM 0
Share

Thank you. Actually, I access to renderer.materials[i].mainTextureOffset in the update function, which is composed of four lines in the form : renderer.materials[1].mainTextureOffset = Vector2 (0,0.offset). I just noticed that removing these lines prevents the materials from being destroyed without having to explicitly save them, so it seems that the problem comes from this access. I will bug it.

avatar image
0

Answer by Bovine · Jan 15, 2011 at 01:12 AM

You should try using sharedMaterials instead of materials. When you access materials you are creating copies. These copies are then discarded because they are not marked as don't destroy. Your fix makes them as such and hence it works.

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

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

No one has followed this question yet.

Related Questions

Changing two different objects renderer colour 1 Answer

How to clean up after changing Renderer.material? 2 Answers

Destroy mesh / material after attaching to renderer 0 Answers

appear/disappear gameobject 3 Answers

Renderer.material not cloning? 3 Answers


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