• 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 /
This question was closed Mar 08, 2015 at 11:50 PM by KnightRiderGuy for the following reason:

Found a solution

avatar image
Question by KnightRiderGuy · Feb 24, 2015 at 10:54 PM · gameobjectbuttonrenderermaterial rendererchange material

Change Object Material With a button C#

I'm trying to figure out how to change the material on an object using a button, so far I have this but I'm not fully understanding the reference to the game object that needs the material changed.

 using UnityEngine;
 using System.Collections;
 
 public class VideoScreenManager : MonoBehaviour {
 
     //Material Reference
     public Material newMaterialRef01;
     public Material newMaterialRef02;
     public Material newMaterialRef03;
     public Material newMaterialRef04;
     public Material newMaterialRef05;
     public Material newMaterialRef06; //Default White Material
 
     public void  GoColor01(){
 
         gameObject.name=("VideoPlayerScreen").renderer.material.color = newMaterialRef01;
     }
 }
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Jessespike · Feb 24, 2015 at 10:57 PM

You're trying to set a color property with a material.

Try this:

 gameObject.name=("VideoPlayerScreen").renderer.material = newMaterialRef01;

Comment

People who like this

0 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 KnightRiderGuy · Feb 24, 2015 at 11:01 PM 0
Share

I get this error Jessespike:

Assets/VideoScreenManager.cs(16,55): error CS1061: Type string' does not contain a definition for renderer' and no extension method renderer' of type string' could be found (are you missing a using directive or an assembly reference?)

avatar image KnightRiderGuy · Feb 24, 2015 at 11:22 PM 0
Share

I even tried putting a public reference to the game object and that still did not work??

avatar image KnightRiderGuy · Feb 25, 2015 at 12:30 AM 0
Share

Maybe it did not like that my VideoPlayerScreen game object was a child of a panel on a canvas because if I drag the script that was on my game manager outside of the canvas and put in directly on the VideoPlayerScreen game object and I just do this this way it works? Well it changes my materials anyway but not doing exactly what I wanted with the Video Texture script. I was trying to see if I could get buttons to change the video screen texture that the web cam plays on because the effect is cool but it does not work the way I want.... but that is another question all together though.

public void GoColor02(){ renderer.material = newMaterialRef02;

 }

It works

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

20 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

Related Questions

How to find the index of the clicked UI button? 1 Answer

Buttons still visible after Destroy() but say "deleted gameobject" in hierarchy 1 Answer

Unable to show gameobjects at specific timing 0 Answers

Destroying all enemy characters after the level is over 4 Answers

Can't remove component. MeshRenderer depends on it 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