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
0
Question by femi · Dec 28, 2010 at 08:53 AM · instantiatematerial

Share material on Instantiate()

I have a script that Instantiates a number of objects. When I look them up in the inspector their materials are called "MaterialName (Instance)" and these seem indeed separate instances of the material - if I tweak the parameters of the original material none of the instantiated objects' appearances change. Is it possible to make the Instantiated objects keep the original material instead?

I tried to set .sharedMaterial after instantiate, like:

var clone : GameObject = Instantiate(proto);
clone.renderer.sharedMaterial = proto.renderer.material;

but it doesn't help. I guess I lack understanding of how .material and .sharedMaterial work.

May be the problem is due to my cloning logic (clouds[ ] is an array of clones):

// clone the gameObject this script is connected to... var proto = Instantiate(gameObject); // make sure this script won't be called on clones proto.active = false; // make clones of the proto-clone for(var i=0; i<clouds.length; ++i) { clouds[i] = Instantiate(proto, transform.position, transform.rotation); clouds[i].transform.parent = transform; clouds[i].name = "Cloud LOD"+i; clouds[i].active = false;

         // remove this script from the clones       
         Destroy(clouds[i].GetComponent(CloudController));
         // [... further cloud setup ...]
 }
 // destroy the proto-clone
 Destory(proto);

The reason it's like this is that I want the artists to be able to place and manipulate the prototypes in the editor, then have them spawn their clones at run-time. Among other things they may change the material used for this particular prototype (but also other things, like particle system parameters).

May be that's what I'm doing wrong and there is a better way?

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 femi · Dec 28, 2010 at 10:38 AM 0
Share

here are screen shots of the inspector:

http://www.flickr.com/photos/artm/5299848182/ http://www.flickr.com/photos/artm/5299247737/

You can see that the second one (with clone selected) has a private instance of the material

1 Reply

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

Answer by Mike 3 · Dec 28, 2010 at 09:40 AM

The issue is that when you read .material from proto, it'll create an instance. I don't think the second line is necessary at all though - it should use the same sharedMateral as the original prefab. What you need to do is make sure that you're using .sharedMaterial to manipulate any colour or texture changes

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 femi · Dec 28, 2010 at 09:52 AM 0
Share

Yes, the second line was there to try to understand what .shared$$anonymous$$aterial means. I still don't get it though.

Is the editor using .shared$$anonymous$$aterial? Because that's what I want to do - to spawn a number of clones then tweak the material in game mode. But what happens is that each of the clones seems to have its own private instance of the material (at least from editor's point of view), while objects that I create manually all share a material (and change accordingly when I tweak a material in the editor in game mode)

avatar image Mike 3 · Dec 28, 2010 at 10:08 AM 0
Share

The editor only manipulates .shared$$anonymous$$aterial - in your case, calling .material at all will cause a new instance to be created, which you don't want

avatar image femi · Dec 28, 2010 at 10:19 AM 0
Share

I added a larger snippet of my cloning logic to the original question with the offending line removed. This still creates new instances of materials.

avatar image femi · Dec 28, 2010 at 10:53 AM 0
Share

$$anonymous$$y bad. I was indeed using .material elsewhere in the script, which is why they all got disconnected. Thanks for pointing me in the right direction.

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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How I can get an instace of a material, created in a project panel 1 Answer

Instantiate GUI text 1 Answer

Terrain with lots of objects 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