• 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 veclock · Mar 26, 2012 at 07:07 PM · c#materialcolor

Simple question about materials

Hello!

I've googled alot and tried many things but I still can't change color of a cube. I use C#, where I create a cube through GameObject.CreatePrimitive(PrimitiveType.Cube). I want it to be a specific color, in this case RGB(30, 225, 86). What's the shortest code for doing that? Also I wonder, if you create a material manually in the editor, how can you apply that material to an object through scripting?

Thanks!

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 · Mar 26, 2012 at 07:12 PM

Did you read this? http://unity3d.com/support/documentation/ScriptReference/Material.Material.html

To set it in the editor, try setting the sharedMaterial rather than the material on the renderer.

Be sure to see this line:

 cube.renderer.material = new Material (Shader.Find(" Glossy"));
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 veclock · Mar 27, 2012 at 02:44 PM 0
Share

Yes, so I wrote this:

 Shader shader = new Shader();
 Color color = new Color(30.0f, 225.0f, 86.0f);
         
 cube.renderer.material = new $$anonymous$$aterial(shader);
 cube.renderer.material.color = color;

But when I run it, I get this error:

NullReferenceException UnityEngine.$$anonymous$$aterial..ctor (UnityEngine.Shader shader) (at C:/BuildAgent/work/b0bcff80449a48aa/Runtime/ExportGenerated/Editor/Graphics.cs:1114) Simulation.createFood () (at Assets/Simulation.cs:60) Simulation.addRandomFood () (at Assets/Simulation.cs:66) Simulation.spawnFood () (at Assets/Simulation.cs:36) Simulation.Start () (at Assets/Simulation.cs:22)

If I delete the two last lines, the code works. What could be wrong?

avatar image DaveA · Mar 27, 2012 at 06:58 PM 0
Share

Edited my answer

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Changing two different objects renderer colour 1 Answer

How to FadeIn 3dObject using alpha color? 1 Answer

Assigning HDR color to material property via script 4 Answers

Changing GUI Text colour when health is low 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