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
4
Question by JasonM · Oct 18, 2011 at 08:49 PM · editormaterialcolor

Change material.color in code and revert on Quit

Hi everyone,

I'm using something like this to change the color of several objects.

skinMat.color = color;

skinMat is a Material attached in the inspector. The problem is when I change the color in-game, it keeps it afterwards. I've tried a bunch of things and no matter what it always keeps the color I chose in-game. I'd like to have the material revert back to its original state when I stop the game in the editor. There MUST be a way to do this, in code or otherwise.

Thanks in advance,

Jason

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

2 Replies

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

Answer by Eric5h5 · Oct 19, 2011 at 04:18 PM

You're better off instantiating the material.

 var mat : Material;
 private var matInstance : Material;
 
 function Start () {
     matInstance = Instantiate(mat);
     renderer.material = matInstance;
     matInstance.color = Color.blue;
 }

That way the original material is never touched, so if something ever goes wrong and OnApplicationQuit is never called (e.g. you accidentally write an infinite loop and have to force-quit, a crash, whatever), it's not a problem.

Comment
Add comment · Show 4 · 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 JasonM · Oct 19, 2011 at 05:06 PM 0
Share

yeah this is an ok way to do it, the issue with this is that you only change the material on 1 gameobject. dave's solution works better for me because any gameobject with that material will show changes. I dont think theres a way to achieve this your way, without looping through everything or using tags. thats not worth the trouble.

avatar image Eric5h5 · Oct 19, 2011 at 06:14 PM 0
Share

@Jason$$anonymous$$: no, if you just assign the instantiated material to all GameObjects that should have it, then changing the instantiated material will change it on all of those GameObjects.

avatar image JasonM · Oct 21, 2011 at 04:03 PM 0
Share

@Eric5h5 That would work, but my goal here is to avoid tracking all those gameobjects at all, theres too many. you have to assign the instantiated material at runtime, so gathering up those gameobjects would be quite a chore.

avatar image Fattie · Jan 15, 2012 at 08:20 AM 0
Share

@Eric, thanks for this great tip. I was wondering, do you understand why the following does NOT work: if you use something like Resources.Load, to essentially "reload" the thing in question, you'd think it would load the Texture2d (say) from the disk and give you a fresh copy to it just as when you last began Play.

But it does not. It just stays with your current modified version. (You can even load something different in between to demonstrate that Rsources.Load is actually doing something) In fact, if you quit Unity and restart it, the resource is in fact unchanged on the disk.

So it seems strange to a Unity newbie like me that using Resources.Load "in flight" appears to just give you a new copy of the thing in question as you have modified it as you go along. Your amazing tip above works perfectly - thank goodness - one can just make an Eric-like instantiated copy of the thing when you start up, and revert to that when desired.

but I'm wondering if you know why "reloading from disk" does not work. (Also I guess, it's a shame to use memory [if it does!] on a copy, when you could just pull it off the disk again, one would assume, with Load). In short to repeat: do you know why Rsources.Load appears to NOT go back for the original on the disk, and indeed can you force it to do so in some way? Thanks!!

avatar image
3

Answer by DaveA · Oct 18, 2011 at 11:58 PM

You could save it off on Start and restore it in OnApplicationQuit http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnApplicationQuit.html

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 JasonM · Oct 19, 2011 at 03:08 PM 0
Share

Thanks, I asked a coworker 10sec after posting, he told me the same thing. exactly what I was looking for! thanks!

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

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Material Color only changes when expanding material tab in inspector. 2 Answers

Color in editor doesn't match up with image 0 Answers

Is there a way to mass assign materials? 4 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