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 radonthetyrant · May 20, 2014 at 12:12 AM · prefabmaterialinspectorrendererinstance

Change material inside instanced prefab, gameobject.renderer is NULL

I use an xml file to keep track of which prefabs I want to assign to which gabeobjects, which I instantiate at runtime, so the level gets generated dynamically.

So I load the prefabs with this line of code into my asset libary:

 GameObject roomCornerTile = Resources.LoadAssetAtPath<GameObject>(roomCornerTilesPaths[i]);

I set the GameObject var of one of my objects with this line of code later on:

 return GameObject.Instantiate(roomCornerTile) as GameObject;

After that, I perform transformations on it to place it in the actual world. Note, that the prefab and materials are loaded like it should and everything works as intended.

Now I want to replace the Material channel 0 which contains a transparent material by prefab definition so I can set it to a colored material according to my needs. This happens here:

 blueprintValidMaterial = Resources.Load ("Materials/TestMaterials/BlueprintValidMaterial") as Material;
 tile.mesh.renderer.materials[0] = blueprintValidMaterial;

Problem is, that exactly at this point, the renderer is NULL and I can't modify any materials or assign new ones.

Question:

How do I have to instantiate a prefab and modify it so I can change materials from an instanced prefab gameobject?


Here you can see the inspector of the prefab and the instanced gameobject. Note that the instanced object has a renderer in the inspector, but is missing the 2nd material channel.

alt text

I could not come up with a solution, please help!

1.jpg (151.6 kB)
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
0

Answer by Jeff-Kesselman · May 20, 2014 at 12:16 AM

what is "tile" in the above code?

If its the prefab then no, you can't do that. What you need to do is Instantiate the prefab and then change the material on the instance.

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 radonthetyrant · May 20, 2014 at 12:47 AM 0
Share

Tile is my own class which has an attribute of type GameObject which is called mesh. This is supposed to be the visible gameobject of this tile which is part of a huge grid.

avatar image Jeff-Kesselman · May 20, 2014 at 12:50 AM 0
Share

How is the mesh field getting set? If you are stuffing a prefab into it, see above.

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

Is it possible to change the colour of game objects without increasing the number of draw calls? 1 Answer

Copying material destroys prefab 1 Answer

How can I set the material on an instance after creating it? 1 Answer

Compare if object is assigned specific Material 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