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
2
Question by Adherbal · Aug 09, 2013 at 04:27 PM · gameobjectruntimesharedmaterial

Assigning sharedMaterial to object built at runtime

I'm creating a number of GameObjects at runtime using:

 new GameObject ("NewObject");

and then creating its mesh (vertices, triangles, ...) and components in script. All this works fine, but I'm trying to figure out how to assign one shared material to all these objects. When changing for example the texture of this material, I want it to change in all the objects using this material (and hopefully save some drawcalls in the process).

I tried using:

 object.renderer.sharedMaterial = material;

But that still seems to create a difference instance for each object?

Any help would be appreciated.

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 IgorAherne · Aug 09, 2013 at 04:54 PM 0
Share

if you are trying to go with batching, would be really interesting. Subscribed to question

2 Replies

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

Answer by Adherbal · Aug 09, 2013 at 06:11 PM

I found the reason of my problem. After assigning a sharedMaterial I was doing

 if (object.renderer.material.mainTexture.width > ...)


Apparently just referring to mainTexture is enough to "break" the sharedMaterial and create an instanced material instead.

So I changed it to:

 if (object.renderer.sharedMaterial.mainTexture.width > ...)

Now changing the material's texture changes the appearance of all objects, as intended. However I did not get any saved drawcalls, but I'll look into that later.

Comment
Add comment · Show 2 · 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 sotirosn · Oct 25, 2013 at 10:44 PM 1
Share

Yep, i was checking if there was transparency in my material as..

 public bool isOpaque {
     get { return renderer.material.color.a == 1; }
 }

ins$$anonymous$$d of..

 public bool isOpaque {
     get { return renderer.shared$$anonymous$$aterial.color.a == 1; }
 }

and that was ruining batching.

avatar image Finjitzu · Nov 29, 2016 at 05:01 PM 0
Share

yup I was just checking the name of the material and that was enough to create an instance. Thank you!!!

avatar image
0

Answer by pitimoi · Aug 09, 2013 at 05:15 PM

Hi,

Try to asign the rendere.material instead of sharedMaterial. The 'material' variable is the material instance used during runtime.

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

17 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

Related Questions

grab all game objects with a distance of the object? 2 Answers

Assign gameObject to a variable at runtime 2 Answers

How to display my castle name at runtime 3 Answers

GameObject.Find vs Resources.Load 1 Answer

Getting a camera at runtime 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