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 Ikiosta · Jun 30, 2015 at 10:16 PM · arrayloopforeach

Foreach with GameObject.Find()

Whenever I click on a button that's supposed to switch between one of six panels that contain their own series of buttons using UnityUI, I get the following error:

NullReferenceException: Object reference not set to an instance of an object button_selectedC.CanvasReset () (at Assets/Scripts/button_selectedC.cs:25)

I've been trying to get this to work for hours, but cannot figure out what I'm doing wrong.

If I use Debug.Log(name); , the strings stored in the array pop up as I expect them to.

 void Start(){
     panelNames[0] = "Panel_mapButtons";
     panelNames[1] = "Panel_inventoryButtons";
     panelNames[2] = "Panel_characterButtons";
     panelNames[3] = "Panel_shipButtons";
     panelNames[4] = "Panel_recordsButtons";
     panelNames[5] = "Panel_unknownButtons";

     }
 
     void CanvasReset (){
         foreach(string name in panelNames){
             Debug.Log (name);
             GameObject.Find (name).GetComponent<CanvasGroup>().alpha = 0;
             GameObject.Find (name).GetComponent<CanvasGroup>().interactable = false;
             GameObject.Find (name).GetComponent<CanvasGroup>().blocksRaycasts = false;
         }
     }

CanvasReset() is called whenever one of the buttons that switches between the panels is clicked.

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 Hellium · Jun 30, 2015 at 10:21 PM

Clearly, I would advise you to use a public array of GameObjects and drag & drop your objects into it. There are multiple advantages :

  • It's far more flexible. If you want to change the name, the position in the hierarchy, add or remove an object, you don't have to do it in your code

  • You don't have to deal with GameObject.Find which is a CPU-intensive function and it's a source of problems like now

If you still want to use the GameObject.Find method, make sure your objects are active in the hierarchy (parent + themselves). Also, if you can give a "path" from the root "node" of the hierarchy or from your transform, it could solve your problem.

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 Ikiosta · Jun 30, 2015 at 10:59 PM 0
Share

After a bit of experimenting, I managed to get the following:

 public GameObject[] myObjects;
     
     void CanvasReset (){
         foreach(GameObject go in myObjects){
             go.GetComponent<CanvasGroup>().alpha = 0;
             go.GetComponent<CanvasGroup>().interactable = false;
             go.GetComponent<CanvasGroup>().blocksRaycasts = false;
         }

And it works! Thanks for suggesting I use a public array of GameObjects!

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

How can I do a foreach loop for an array of booleans? 1 Answer

Checking an array variable in C# vs JavaScript 3 Answers

Array Element in Foreach loop 1 Answer

Using ForEach Loop to change variables on prefabs 1 Answer

Raycasting and 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