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 jansirani · Jul 29, 2013 at 12:49 PM · gameobjectcolorarrays

How to set color to gameObject respective to below coding?

var prefab : GameObject; var numberOfObjects = 10; var radius = 5;

function Start () { for (var i = 0; i < numberOfObjects; i++) {

    var angle = i * Mathf.PI * 2 / numberOfObjects;
     var pos = Vector3 (Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
     Instantiate(prefab, pos, Quaternion.identity);
    
   
     
 

} }

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 brandonsbarber · Jul 29, 2013 at 03:11 PM 1
Share

Several comments on your question. First off, please edit your post so that your code tags fit around the entire code block. This tool can be found via the button that reads "101010".

Also, don't just put your question in the header. Please put the question and a little more relevant information in the actual question body.

I would recommend changing this title to something like "Setting Prefab Color Programmatically"

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Lovrenc · Jul 29, 2013 at 12:50 PM

 renderer.material.color = yourColor
Comment
Add comment · Show 1 · 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 jansirani · Jul 29, 2013 at 02:45 PM 0
Share

i have 10 gameobjects , each of gameobject should have diffrent color.how could i do this pls some one help me?

avatar image
0

Answer by brandonsbarber · Jul 29, 2013 at 03:26 PM

Assuming you already know all of the colors you need, I would just make an array of Color objects (`Color[]`) that is equal in length to the number of objects you need.

Set all of these colors (either with an initializer list, a loop, or manually). Then, inside of your loop in Start, you need to change a few things. First off, save the result of Instantiate to a GameObject. To do this, change your line to save it to a GameObject variable.

 var myObject : GameObject = Instantiate(prefab, pos, Quaternion.identity);

Essentially, this tells myObject that it is a GameObject, and then sets it to be whatever you just created. After this, simply call

 myObject.renderer.material.color = myColorArray[i]; //myColorArray is the previously mentioned color array

and you should be all set.

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

17 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

Related Questions

Transparency cube shader? 1 Answer

Basic object creation in arrays! Syntax error! 1 Answer

How to Narrow Down an Array casted from FindObjectsOfType 1 Answer

Array of Arrays of GameObjects/Prefabs (C#) 1 Answer

How could I collect all gameobject Materials? 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