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 /
avatar image
0
Question by vpg260 · Apr 04, 2020 at 04:20 PM · scripting problemarraynot workingforeach

Foreach loops only runs once, on one object in an array.

Hey everyone,

This particular issue has had me at wit's end for a while now and I'm hoping someone here can push me into the right direction.

The situation is as follows, I have two objects (simple duplicate cubes) and have one script for both, both are in an array (_player.waterFalls)

When I jump from my Player script, I tell this script to set one of the cubes to isTrigger, and the other to isTrigger is false.

This has consistently worked perfectly for one of the cubes. The other unfortunately has remained entirely inanimate and doesn't even print my Debug.Logs.

It is as if there isn't even a script on there.

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Waterfall : MonoBehaviour
  {
      [SerializeField]
      private bool frozen;
  
      [SerializeField]
      private BoxCollider2D collider;
      private Player _player;
  
      // Start is called before the first frame update
      void Start()
      {
          collider = GetComponent<BoxCollider2D>();
          _player = GameObject.Find("Player").GetComponent<Player>();
      }
  
      // Update is called once per frame
      void Update()
      {
          foreach (GameObject waterfalls in _player.waterFalls)
          {
              if (frozen)
              {
                  gameObject.layer = LayerMask.NameToLayer("Ground");
                  collider.isTrigger = false;
              }
  
              else if (!frozen)
              {
                  gameObject.layer = LayerMask.NameToLayer("Default");
                  collider.isTrigger = true;
              }
          }
      }
  
      public void FreezeUnfreeze()
      {
          foreach (GameObject waterfalls in _player.waterFalls)
          {
              if (frozen)
              {
                  frozen = false;
                  Debug.Log(name);
                  break;
              }
  
              else if (!frozen)
              {
                  frozen = true;
                  Debug.Log(name);
                  break;
              }
          }
      }
  }

To summarize, the FreezeUnfreeze proc above does exactly what it's supposed to do, but only for the first cube I attached the script to.

And yes, I have them correctly loaded up in the inspector and applied the scripts.

Additionally, this is how I reference and call the script.

 waterFall = GameObject.Find("Waterfall").GetComponent<Waterfall>();

  waterFall.FreezeUnfreeze();

Thanks for reading.

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 n_rusev · Apr 04, 2020 at 04:25 PM

Hello. Your problem might be that GameObject.Find("Waterfall").GetComponent(); returns only one object. You can instead create a tag "Waterfall" and call like so:

     foreach(GameObject waterfall in GameObject.FindGameObjectsWithTag("Waterfall"))
     {
         waterfall.GetComponent<Waterfall>().FreezeUnfreeze();
     }

Comment
Add comment · Show 3 · 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 vpg260 · Apr 04, 2020 at 06:19 PM 0
Share

Unfortunately that didn't change anything, thanks for the suggestion though.

avatar image vpg260 · Apr 04, 2020 at 06:23 PM 0
Share

Upon further (mild) tinkering, it worked out after all, thank you so much! This stuff was driving me crazy for days!

avatar image vpg260 · Apr 04, 2020 at 06:26 PM 0
Share

For future readers;

N_rusev's solution but omitting ''GetComponent()'''from n_rusev's code solved it for me.

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

238 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 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

Script wont work for me please give me answers or a new script,camera folllow script is very janky and twitchs alot please help 2 Answers

Unity Script not working in build, but does work in play mode 1 Answer

How do I compare the position of my player to each elements position of an array of GameObjects? 0 Answers

Array problems 3 Answers

Vuforia enabled scripting define 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