Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Andrew_Rollins · Sep 06, 2016 at 04:37 PM · prefabrenderingmaterial

change material of one prefab

Hi,

I'm working on a split-screen game where each player is a prefab of the player object. The player prefab has several materials applied to the same object, and I want to change only one through script.

Currently in-script I have a public material which I have dragged the target material (called goo) into from the inspector. The code I have changes the material like I wanted, but it changes it for every instance, and I only want it to change it for the objects that call the code. Also side note, my current code changes the color permanently, even when I reopen the program.

Thanks for your help!

H. Chappee

Comment
Add comment · Show 4
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 Guppie1337 · Sep 07, 2016 at 06:13 PM 0
Share

I believe the issue you're running into is changing the base object of the prefab. They get instantiated with the values associated with the prefab in your project. If you want to change anything specific to the player while they share the same prefab, you would need to create a reference to the instance and not the prefab.

e.g. I make 2 balls. They are both following a base prefab of ball. Ball has an initial color of green, so both balls will be green when they are created because they are referencing the object to be cloned from. To change the color, you need a reference to the instance of each. You can do that by instantiating the objects and casting them as a GameObject and store them in their own variable. This will allow you to access the color of each specific ball made.

I gave my 2 cents to potentially clarify any issues you had with understanding the answers provided. @Hanoble pretty much answers it for you.

avatar image Andrew_Rollins Guppie1337 · Sep 08, 2016 at 01:59 AM 0
Share

I did that, and it seems to work fine, but the issue I'm having is I don't know how to actually change the color of a specific material (given that there are multiple different materials applied to the same mesh) on one of the instances via code.

avatar image Guppie1337 Andrew_Rollins · Sep 08, 2016 at 02:19 AM 0
Share

This may help you. SetColor I believe you can target the material you want. Just make proper references and you should be fine.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JedBeryll · Sep 06, 2016 at 05:49 PM

Then you are changing the prefab value. When you load your prefab (maybe with Resources.Load) you are assigning the material to that object which leads to the behaviour you described. Instantiate it and save the instance in a variable then change the material of the instance.

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 Andrew_Rollins · Sep 06, 2016 at 06:11 PM 0
Share

Thanks for the quick reply!

I'm not really sure how to do that though, would you $$anonymous$$d walking me through it a bit?

avatar image
0

Answer by Hanoble · Sep 06, 2016 at 06:58 PM

When you instantiate the object, you need to do this to get that specific instance.

  GameObject instanceOfPlayer = Instantiate(playerPrefab, spawn.position, Quaternion.identity);

Now you have an instanceOfPlayer object, which is the actual instantiated object, not the prefab. Once you have that you can change the material of that object, by getting the renderer of instanceOfPlayer and changing it there.

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 Andrew_Rollins · Sep 07, 2016 at 05:58 PM 0
Share

That seems to work well for instantiation, but I'm still not really sure how to change the individual material colors. Here's what I have so far:

public GameObject player;

 public player_scr script1;

 public player_scr script2;

 // Use this for initialization
 void Start () {
 
     GameObject Player1 = (GameObject) Instantiate (player, new Vector3 (5, 10, 0), Quaternion.identity);

     GameObject Player2 = (GameObject) Instantiate (player, new Vector3 (-5, 10, 0), Quaternion.identity);

     script1 = Player1.GetComponent<player_scr> ();

     script2 = Player2.GetComponent<player_scr> ();

     script1.bodycolor.SetColor ("_Color", Color.green);

     script2.bodycolor.SetColor ("_Color", Color.blue);

     script1.playernumber = 1;
     script2.playernumber = 2;

 }

Changing the "playernumber" variable works, but changing the "bodycolor" variable (which contains the material I want to target) still changes both. Are there other methods for changing the material that I could try?

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[URP] Rendering an object with a different material behind a transparent plane 0 Answers

Refraction - GrabPass in Forward Rendering 1 Answer

Messy normals with Transparent material 0 Answers

change prefab material 2 Answers

Blue Light/Roughness on the cavity of my model 0 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