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 DaveA · Jan 15, 2011 at 01:54 AM · materialplatformassigncrossplatformsharedmaterial

Setting sharedMaterial only setting for one object?

I'm trying to set materials when the game starts depending on the current platform. I have a bunch of objects sharing, let's say 'Steel' and for beefy platforms want to set it to 'Steel2'

I thought I could simply find one object using Steel, get it's renderer.sharedMaterial and assign to Steel2. This works fine, for one object. The rest are not affected, despite the explicit explanation that they should be in the Scripting Reference.

What am I missing?

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
2
Best Answer

Answer by Bunny83 · Jan 15, 2011 at 02:11 AM

You just changed the reference to point to another material. The sharedMaterial reference points to a Material within your assets. If you change this reference to another Material, the old Material isn't changed at all.

You could use

renderer.sharedMaterial.CopyPropertiesFromMaterial(mat1);

but watchout! that will change the material that is bound to this renderer and the old information is lost. If you plan something like that i would create 3 materials:

  1. That is used for the renderer and that is changed at runtime
  2. Your "Steal" material
  3. Your "Steel2" material

ps. the renderer.material is a local copy inside the renderer, if you change that the original material will not be changed.

*edit*15.01@04:00 Ok i've tested it with 2 spheres. Both have my "sharedMat". On one sphere i put this script (C#):

public class changeMat : MonoBehaviour
{
    public Material Mat1;
    public Material Mat2;
    void OnGUI()
    {
        if(GUILayout.Button("Mat1"))
        {
            renderer.sharedMaterial.shader = Mat1.shader;
            renderer.sharedMaterial.CopyPropertiesFromMaterial(Mat1);
        }
        if(GUILayout.Button("Mat2"))
        {
            renderer.sharedMaterial.shader = Mat2.shader;
            renderer.sharedMaterial.CopyPropertiesFromMaterial(Mat2);
        }
    }
}

you have to assign the 2 different Materials to Mat1 and Mat2 in the editor. When you press one of the buttons the material get changed for both of my spheres and the sharedMat (that's the one i've assigned to the spheres) is changed (forever even when you stop the game).

Comment
Add comment · Show 5 · 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 DaveA · Jan 15, 2011 at 02:23 AM 0
Share

That sounded like a good idea, but it's still not working, and now, I don't even get a change on the object found. I take it the documentation is wrong? I really want to avoid having to crawl my whole scene looking for materials to replace.

avatar image Bunny83 · Jan 15, 2011 at 02:45 AM 0
Share

I just tried it, it works great for me ;) maybe something else is wrong. I just tried it with materials without texture, let me try this first. brb. ;)

avatar image Bunny83 · Jan 15, 2011 at 03:06 AM 0
Share

I've tested it with textured and it works find. everything got copied except one thing: the used shader. That's why i copy the shader manually before i copy the properties in my example.

avatar image DaveA · Jan 17, 2011 at 08:08 PM 0
Share

Setting the shader was the key. In fact, I didn't need to use CopyPropertiesFrom$$anonymous$$aterial after it. Thanks!

avatar image DaveA · Jan 17, 2011 at 08:57 PM 0
Share

Spoke too soon. I ended up needing CopyProp... after all for more complex shaders. Anyway, all is well. I can have the same basic assets build for any platform, picking shaders/mats on-the-fly.

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

Why isn't (.)sharedMaterial sharing this change to "_EmissionColor"? 0 Answers

Network Question. Sending value from iPhone Client to Mac Server 1 Answer

Two questions about building particle systems from scripts 1 Answer

Nintendo 3ds platform support 1 Answer

Mass assign shaders? 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