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 Abhi94 · Jun 13, 2015 at 07:05 PM · gameobjectscript.access

How can i access all the objects with whom my script is attached to ???

I am only able to access only first child( there are 5 more) , i want to access all 6 at once . I am talking about this line ChangeColor ChangeColor1 = thePlayer.GetComponentInChildren();

  using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class onClick1 : MonoBehaviour {
 
     public Text ScoreText;
     private int Count = 10;
     private int wrongAnswer = -5;
     public bool hello;
 
     public    void SayHello(  bool hello = false ) {
 
         
       if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
 
             GameObject thePlayer = GameObject.FindGameObjectWithTag("GameController");
             DisplayColor displayColor = thePlayer.GetComponentInChildren<DisplayColor>();
         
 
             ChangeColor ChangeColor1 = thePlayer.GetComponentInChildren<ChangeColor>();
 
 
             hello = true;
             displayColor.changeColor();
             ChangeColor1.ColorME();
             ScoreText.text = "SCORE: " + Count;
 
         
         
         
         } else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
     
             hello = false;
             ScoreText.text = "SCORE: " + wrongAnswer;
         
         }
         }
     
 }

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 Cherno · Jun 13, 2015 at 08:18 PM

You are using GetComponentInChildren. This will only return one of the specified component. What you need to use instead is GetComponentsInChildren. Note the "s" indicating plural ;)

Edit: The complete code:

  DisplayColor[] displayColors = thePlayer.GetComponentsInChildren <DisplayColor>();
   
   foreach(DisplayColor displayColor in displayColors) {
        Debug.Log(displayColor.gameObject.name);
        displayColor.changeColor();
           
   }
 
    ChangeColor[] changeColors = thePlayer.GetComponentsInChildren <ChangeColor>();
       
       foreach(ChangeColor changeColor in changeColors ) {
            Debug.Log(changeColor .gameObject.name);
            changeColor.ColorME();
       }
Comment
Add comment · Show 10 · 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 Abhi94 · Jun 14, 2015 at 07:36 AM 0
Share

Its not working ,i am getting this error - CS0029: Cannot implicitly convert type ChangeColor[]' to ChangeColor'

avatar image Cherno · Jun 14, 2015 at 07:43 AM 0
Share

Take a look at the scripting API I linked to again. GetComponentsInChildren returns an array of components, which makes sense since you want to be able to get more than one.

 DisplayColor[] displayColor = thePlayer.GetComponentsInChildren<DisplayColor>();
 ChangeColor[] ChangeColor1 = thePlayer.GetComponentsInChildren();
 

You would then be able to itereate through the array to access one of the stored components.

avatar image Abhi94 · Jun 14, 2015 at 07:59 AM 0
Share

ok i have tried but i still getting this error- CS1061: Type ChangeColor[]' does not contain a definition for Color$$anonymous$$E' and no extension method Color$$anonymous$$E' of type ChangeColor[]' could be found (are you missing a using directive or an assembly reference?)

Now it cant locate my "Color$$anonymous$$E " function

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class onClick1 : $$anonymous$$onoBehaviour {
 
     public Text ScoreText;
     private int Count = 10;
     private int wrongAnswer = -5;
     public bool hello;
 
     public    void SayHello(  bool hello = false ) {
 
         
       if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
 
             GameObject thePlayer = GameObject.FindGameObjectWithTag("GameController");
             DisplayColor displayColor = thePlayer.GetComponentInChildren <DisplayColor>();
         
 
             ChangeColor[] ChangeColor1 = thePlayer.GetComponentsInChildren<ChangeColor>();
 
 
             hello = true;
             displayColor.changeColor();
             ChangeColor1.Color$$anonymous$$E();
             ScoreText.text = "SCORE: " + Count;
 
         
         
         
         } else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
     
             hello = false;
             ScoreText.text = "SCORE: " + wrongAnswer;
         
         }
         }


avatar image Cherno · Jun 15, 2015 at 11:00 PM 1
Share

Sorry, I used GetComponentInChildren ins$$anonymous$$d of GetComponentsInChildren :rolleyes:

 DisplayColor[] displayColors = thePlayer.GetComponentsInChildren <DisplayColor>();
  
  foreach(DisplayColor displayColor in displayColors) {
       Debug.Log(displayColor.gameObject.name);
       displayColor.changeColor();
          
  }

   ChangeColor[] changeColors = thePlayer.GetComponentsInChildren <ChangeColor>();
      
      foreach(ChangeColor changeColor in changeColors ) {
           Debug.Log(changeColor .gameObject.name);
           changeColor.Color$$anonymous$$E();
      }
 
avatar image Cherno · Jun 22, 2015 at 05:28 PM 1
Share

How do you know I'm not a girl? Well, I'm not so don't get your hopes - or anything else - up :P

I had no internet for the last week but I'm glad that you found my help... helpful! I pasted the final, correct solution into the original answer. Feel free to mark it as accepted to help otehr users sharing the same problem.

Edit: About the lag: Note the the "Find(...)" functions are pretty resource-intensive. It might be better to just store the desired GameObject in it's own variable at game start.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Accessing other GameObjects Script Variables 2 Answers

How to create gameobject with different setting 1 Answer

Hi im trying to set a Gameobject variable in a script not in the inspector. 3 Answers

Access main camera from different GameObject script 1 Answer

Prefab Script List not being assigned as unique - can you see why? 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