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 aljndrrr · Apr 12, 2012 at 05:51 PM · gameobjectmaterials

Changing a material using Unity Editor Script

I have the following game object

 MyGameObject
 -MeshRenderer
 ----Materials:
 ----- Size: 1
 ----- Element 0: MyGameObjectMaterial

I want to change the gameobject material using a Unity Editor Script, my script looks similar to this

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 
 public class BuildingAutomator : EditorWindow
 {
     public GameObject myGameObject;
     public Material myNewMaterial;
     
     // Add menu named "My Window" to the Window menu
     [MenuItem("Window/My Window")]
     static void Init()
     {
         // Get existing open window or if none, make a new one:
         BuildingAutomator window = (BuildingAutomator)EditorWindow.GetWindow(typeof(BuildingAutomator));
     }
 
     void OnGUI()
     {
         if(GUILayout.Button("Change material!"))
         {
             myGameObject.renderer.materials[0] = myNewMaterial; //<--- Error
         }
     }
 }

But it throws the following error:

 Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene. You most likely want to use renderer.sharedMaterial instead.
 UnityEngine.Renderer:get_materials()

Any suggestions?

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 DaveA · Apr 12, 2012 at 06:40 PM

You most likely want to use renderer.sharedMaterial instead.

myGameObject.renderer.sharedMaterials[0] = myNewMaterial;

Comment
Add comment · Show 3 · 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 aljndrrr · Apr 12, 2012 at 07:25 PM 0
Share

It doesn't throws any errors (neither do myGameObject.renderer.materials[0]) but it doesn't assign the material...

avatar image DaveA · Apr 12, 2012 at 11:48 PM 0
Share

How are you making myNew$$anonymous$$aterial?

avatar image aljndrrr · Apr 13, 2012 at 03:56 AM 0
Share

I create the material on the "Project", then assign it a shader. Because myNew$$anonymous$$aterial is public I just drag and drop it from the "Project".

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

One fraction of game object one material, other fraction different material? 1 Answer

Add mesh with materials to scene through code. 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Beginner Question: Player Owned GameObject 1 Answer

Return All gameobjects in scene to an array 4 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