• 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
3
Question by JoeStrout · Feb 01, 2011 at 06:57 PM · textureinstantiatematerialclone

How to independently change textures on clones in C#?

I'd like to have a prototype object, which I clone at runtime (possibly many times) and then change the texture on each one independently. My early experiments were in JavaScript, but now I'm moving to C# for easier multidimensional arrays.

In JavaScript, this works fine:

panel = Instantiate(panelPrototype, Vector3(0,-0.5,0), Quaternion.Euler(0,0,180));
panel.renderer.material.mainTextureOffset = Vector2(col/16.0,  (15-row)/16.0);

I can call this multiple times with different row and col values, and each one gets a unique look. But when I try to do the same thing in C#:

panel = (GameObject)Instantiate(panelPrototype, new Vector3(0,0.5f,0), Quaternion.identity);
panel.renderer.material.mainTextureOffset = new Vector2(col/16.0f, (15-row)/16.0f);

...the result appears to be panels that are linked together. Both the original and the clone always have the same look, and even if I change the texture offset in the editor for one, it changes it for the other.

I would speculate that this is because Instantiate did not do a deep copy, but instead gave me a clone that's sharing the same material object as the original. If there's only one material object, then of course all objects using it will look the same. But I thought that JavaScript and C# were calling through to the same stuff under the hood. So why is the behavior different? And, how would I go about cloning an object and giving it a unique look in C#?

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

Answer by Lemon · Feb 08, 2011 at 10:19 AM

Just had a similar problem. You are correct in assuming that this happens because a deep copy was not performed. You can force the deep copy yourself:

panel.renderer.material = Instantiate (panel.renderer.material) as Material;

note that you may in turn need to copy the texture for the Material as well:

panel.renderer.material.mainTexure = Instantiate (panel.renderer.material.mainTexture) as Texture2D;

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 JoeStrout · Feb 09, 2011 at 03:13 PM 0
Share

Thanks, that's a neat trick and sounds like it'll solve the problem.

I still find it disturbing that the same function behaves differently in JavaScript vs. C#, though!

avatar image
0
Wiki

Answer by $$anonymous$$ · Jun 15, 2015 at 09:46 AM

If anyone has this problem with materials being shared by default, this might help:

 void Awake() {
         Text t = GetComponentInChildren<Text>();
         //  Ensure that this material is NOT the shared one!!!
         Material m = t.material;
         t.material = new Material(m);
 }

This helped solve an issue I had where a Canvas Text material was using the shared material, and I wanted it to use its own independent material.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to assign different TEXTURES to objects with the same mesh? 1 Answer

Dynamic Material & Textures 1 Answer

Cant get font type in textures 0 Answers

Attaching scripts to meshes with multiple materials 2 Answers

How to stop instantiate spawn ? 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