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 jimjames · Feb 08, 2015 at 05:53 PM · getcomponentsinchildren

GetComponentsInChildren Problem

Dear: Reader

What I am trying to do is have all my objects under "playModeUI" that have the script "dragAndDropUI" to run there function "TurnOnDragUI()".

Script "A"

 using UnityEngine;
 using System.Collections;
 
 public class handlerUI: MonoBehaviour 
 {
     public GameObject[] playModeUI;
 
     public void TurnOnDragUI() 
     {
         playModeUI = GameObject.GetComponentsInChildren<dragAndDropUI>();
         
         foreach (dragAndDropUI script in playModeUI) 
         {
             script.TurnOnDragUI();
         }
     }
 }


James

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

Answer by daneislazy · Feb 08, 2015 at 06:02 PM

So I'm gonna say that that doesn't work because the array elements are GameObjects not dragAndDropUI objects. This is because the array was made as GameObject[]. So, instead make your array like this:

 public dragAndDropUI[] playModeUI;

If that doesn't work you can also use the Monodevelop type, but that will probably give you the same problem. If the GetComponentsinChildren isn't working you can put Monodevelop into that as well, but that will return all scripts.

Edit: Final Code from comments, with additional notes from me

  using UnityEngine;
  using System.Collections;
  
  public class togglePanelUI : MonoBehaviour 
  {
  //public GameObject for referenceing the UI Canvas, drag canvas into variable in inspector
  public GameObject canvasUI;

      public void DragUI()
      {
  // make a new array of the script's class name, 
  // find all components in canvas with the same script/class name
          dragAndDropUI[] tos = canvasUI.GetComponentsInChildren<dragAndDropUI> ();
  // iterate through all of the selected scripts and call the same method on them
          foreach(dragAndDropUI scripts in tos)
          {
              scripts.TurnOnDragUI();
          }
      }
  }


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 jimjames · Feb 08, 2015 at 06:08 PM 0
Share
 public GameObject[] play$$anonymous$$odeUI;

is where i am going to drop my UGUI canvas

I am trying to access all the scripts.

avatar image jimjames · Feb 08, 2015 at 06:12 PM 0
Share

Trying to call the function on all the gameobjects with the "dragAndDropUI" scipt.

avatar image daneislazy · Feb 08, 2015 at 06:20 PM 0
Share

oh, ok. I didn't realize you were using it for something else.

You should just be able to make a new array using the same idea. I had to do some thing similar recently; you need to store the scripts as whatever class they are or as $$anonymous$$onodevelop. If you use $$anonymous$$onodevelop for the array creation and the GetComponentsInChildren call that should let you use the array elements just like a GetComponent would. Probably check the name of the component to see if it is dragAndDropUI or whatever it pirints/debug.logs as.

Im not sure if you can just do script.TurnOnDragUI() at that point, you might need script.dragAndDropUI.TurnOnDragUI().

And make sure the TurnOnDragUI() is public.

Edit: oh also if you use $$anonymous$$onodevelop it will return all UI things like Button Image and Text because they are scripts.

avatar image jimjames · Feb 08, 2015 at 06:28 PM 0
Share

Sorry, but how would I go about in writing that in script? and yes the function is public

avatar image daneislazy · Feb 08, 2015 at 06:34 PM 1
Share

oh, if play$$anonymous$$odeUI is just the one canvas it doesn't need to be an array, assu$$anonymous$$g that:

 dragAndDropUI[] dragArray = play$$anonymous$$odeUI.GetComponentsInChildren<dragAndDropUI>();
          foreach (dragAndDropUI script in dragArray) 
          {
              script.TurnOnDragUI();
          }

Try that.

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

20 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

Related Questions

Get SkinnedMeshRenderer as Renderer? 1 Answer

GetComponentsInChildren() doesn't works like I expect 1 Answer

Invalid Cast error when my format seems the same as the script reference example? 1 Answer

GetComponentsInChildren contains null elements when fetching MeshRenderers 1 Answer

Is it not possible to include "if-then" conditionals within foreach loops? 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