Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by jpierre88 · Jul 02, 2013 at 06:53 AM · javascriptmaterialdynamiccolors

How to dynamically create a cube and color it?

I am a week into Unity and am confused about coloring a dynamic cube in javascript. I've been searching for over 2 hours and can't grasp the right way to do so. I know in logic I have to create the material and give it a color but, I don't know how to assign it to a dynamic cube. I was only able to create a cube for now:

 function Start () {
     var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
     cube.name = 'brown_cube';
 }
Comment
Add comment · Show 1
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 cdrandin · Jul 02, 2013 at 06:56 AM 0
Share

When you create your cube, give it a material then color it. AddComponent($$anonymous$$aterial) https://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$aterial.html

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by SubatomicHero · Jul 02, 2013 at 06:56 AM

It's quite simple:

 function Start() {
     var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
     cube.name = 'brown_cube';
     // color is controlled like this
     cube.renderer.material.color = Color.brown; // for example
     // There are lots more colours to choose
 }

Hope this helps!

Comment
Add comment · Show 8 · 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 jpierre88 · Jul 02, 2013 at 07:03 AM 0
Share

@SubatomicHero It gave me an error: ...'brown' is not a member of 'UnityEngine.Color'. Do you $$anonymous$$d showing me a quick syntax using RGB I guess?

avatar image SubatomicHero · Jul 02, 2013 at 07:07 AM 0
Share

Yes, I $$anonymous$$now brown isn't a color in that class it was just an example.

Read THIS link to see what you can use :D

If you are new at using Unity, I seriously suggest reading the documents online to answer any questions. It's a great place to start.

avatar image fafase · Jul 02, 2013 at 07:13 AM 0
Share

If you need brown you can find the RGB of any color you like online. Then you can "create" your own:

 Color brown = new Color(139f/255f, 69f/255f, 19f/255f, 1f);
 cube.renderer.material.color = brown;

The last parameter of the constructor is the alpha channel, 0 -> transparent, 1 visible and then 0.5f see through. Since your new color is not static, do not use Color.brown but just brown.

See there http://www.tayloredmktg.com/rgb/

avatar image AKnightWhoSaidNIII fafase · Jan 26, 2017 at 05:22 PM 0
Share

hi, I found this page because I was trying to find out how to change the colour of an object using its RGBA value rather than the dam shortcut things that come up on all the documentations. When I tried to replace them with straight RGBA values I got error saying that they couldn't be used in that context so I was glad to finally find your comment but I replaced cube with my variable name and i got the same "does not exist in current context" error. I was wondering if you could help me out.

avatar image fafase AKnightWhoSaidNIII · Jan 28, 2017 at 11:22 AM 0
Share

You'd have to show your code. I recommend to start a new question as this is not relevant to this one.

Show more comments
avatar image jpierre88 · Jul 02, 2013 at 07:14 AM 0
Share

COOL... Thanks!

avatar image
0

Answer by OlivierPons · May 07, 2021 at 05:12 PM

Answers (like almost all answers) are out of date and not working in recent versions of Unity. Here's a working version:

 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
 cube.name = "brown_cube";
 Color brown = new Color(139f/255f, 69f/255f, 19f/255f, 1f);
 // color is controlled like this
 cube.GetComponent<Renderer>().material.color = brown;
Comment
Add comment · 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

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

18 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

Related Questions

How to make good parser to get colours from RGBa and set them to objects 1 Answer

change skybox via script help ? 1 Answer

Apply a texture at startup js 1 Answer

Substance Material - Clone at Runtime (Mobile) 1 Answer

How to Make the colors separate from this sprite like in the original game. 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