Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Hampen555 · Nov 23, 2017 at 05:51 PM · 3dvrprogramming-basics

Same color on all child GameObjects instead of different colors with function

I'm making a scene in VR and I'm having a problem with a GameObject and its children.

So I have a ladder made from 13 parts, and I'm using VRTK to make it grabbable, and I have this function being called whenever I'm holding the ladder and also pressing the trigger button on the rift controller:

 namespace VRTK.Examples
 {
     using UnityEngine;
 
     public class Color2 : VRTK_InteractableObject
     {
         Color[] colors = new Color[6];
 
         void Start()
         {
             colors[0] = Color.cyan;
             colors[1] = Color.red;
             colors[2] = Color.green;
             colors[3] = new Color(255, 165, 0);
             colors[4] = Color.yellow;
             colors[5] = Color.magenta;
         }
 
         public override void StartUsing(VRTK_InteractUse usingObject)
         {
             base.StartUsing(usingObject);
             LadderUse();
         }
 
 
         private void LadderUse()
         {
             GameObject[] objects = GameObject.FindGameObjectsWithTag("Ladder");
             foreach (GameObject go in objects)
             {
                 MeshRenderer[] renderers = go.GetComponentsInChildren<MeshRenderer>();
                 foreach (MeshRenderer r in renderers)
                 {
                     foreach (Material m in r.materials)
                     {
                         if (m.HasProperty("_Color"))
                             m.color = colors[Random.Range(0, colors.Length)]; ;
                     }
                 }
             }
         }
     }
 }

And my problem is that every part of the ladder gets a different color, what should I change to make them all get the same color instead? I'm pretty new to c#, unity and VRTK but everything is working like planned except this annoying part! I really appreciate if someone could help me out :)

One solution I can think of is to make all of the ladder child objects into one object instead, but I also don't know how to do that and I have ofc tried googling but I don't even know what to google in the first place

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
1
Best Answer

Answer by Dragate · Nov 24, 2017 at 01:08 PM

You've put random inside a loop. Move it out to get one random index and reuse it for all iterations.

      private void LadderUse()
      {
          GameObject[] objects = GameObject.FindGameObjectsWithTag("Ladder");
          int randomColorIndex = Random.Range(0, colors.Length);
          foreach (GameObject go in objects)
          {
              MeshRenderer[] renderers = go.GetComponentsInChildren<MeshRenderer>();
              foreach (MeshRenderer r in renderers)
              {
                  foreach (Material m in r.materials)
                  {
                      if (m.HasProperty("_Color"))
                          m.color = colors[randomColorIndex];
                  }
              }
          }
      }
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 Hampen555 · Nov 24, 2017 at 04:26 PM 0
Share

Thank you, worked exactly like I wanted to! :D

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

150 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Antichamber-like shader for lights and outline? 0 Answers

SteamVR Plugin and 3D-technology don't work with each other 0 Answers

Mesh Cutting Metal with jaws-of-life 0 Answers

How to remove this ghost image? double imaging? on Unity 2017.3.1 + SteamVR 1.1.3 + HTC Vive / Vive Pro 1 Answer

OVR Walking Animation trigger. 0 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