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 07, 2020 at 02:55 AM · referenceforeacharray of gameobjects

Script doesn't properly reference.

Hey everyone,

I'm trying to reference two objects in an array, then get their respective individual values to decide whether or not the if statement below should run on them.

The first debug.log properly prints the names (waterfallScript.name) of both objects, but the second debug.log after the if statement only prints the name of the first.

The rest of the code (the transform and coroutine) only work on the first one as well.

It's worth noting that altering the first object does affect the second.

It's like the second object gets lost somehow once I go into the if statement.

 public void Waterfalling()
 {

     foreach (GameObject waterfalls in _player.waterFalls)
     {
         var waterfallScript = waterfalls.GetComponent<Waterfall>();
         Debug.Log(waterfallScript.name);
         if (!waterfallScript.frozen && waterfallGo && !_player.isTurning)
         {
             Debug.Log(waterfallScript.name);
             transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y + fallSpeed);
             StartCoroutine(WaterfallTimer());
         }
     }

 }

Thanks for reading, hopefully someone knows what's going on.

Comment
Add comment · Show 1
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 cwalshwarder · Apr 07, 2020 at 03:22 AM 0
Share

The if statement is only executing for the first. Either the if condition always fails for the second, or when the if statement executes, it changes something which makes it fail. If it always fails for the second, maybe waterfallScript.frozen is true. If not, maybe it's something in the WaterFallTimer coroutine.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zblase · Apr 07, 2020 at 06:26 AM

Your logical condition isn't true when passing in the second object (i.e one of the conditions is false). I'm not sure how the "waterfallGo" and "_player" variables are behaving outside of this block of code, but I would guess the "frozen" variable on the second object is true (making it false with the "!" operator). The easiest way to determine this is to set a breakpoint on line 8 (the if statement), start the project in Visual Studio (or whatever IDE you're using), and run the program in Unity. Once the program hits that line of code, it will pause and you can hover over the "waterfallScript" to see if the "frozen" variable is true or false.

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 07, 2020 at 07:38 AM 0
Share

I have both objects present in the scene.

One object's frozen is true, the other is set to false. The if condition executes on both even though it shouldn't on the second.

When I set the first one to false the second one stops as well. Completely ignores it's own script.

The waterfallgo value belongs to a coroutine in the script that intermittently stops the code because I wanted brief delays.

     IEnumerator WaterfallTimer()
     {
         waterfallGo = false;
         yield return new WaitForSeconds(0.12f);
         waterfallGo = true;
     }

The _player.isTurning variable just makes sure the code doesn't execute when the player is turning, works fine.

The issue really is the second GameObject not checking it's own frozen value.

Acts like I never even bothered to get it.

avatar image zblase vpg260 · Apr 07, 2020 at 07:54 AM 0
Share

The foreach statement will continue to loop even if you're waiting x seconds in the coroutine. Ergo, when the first loop calls the WaterfallTimer coroutine, it's setting "waterfallGo" to false, so when your second loop hits that if statement, it won't execute the code inside said if statement because "waterfallGo" is false. If you remove the "waterfallGo = false", the second object will execute the code inside the if statement. I'm sure that's probably not the exactly functionality you're looking for but that's not something I completely understand at the moment. If you want the second object to execute that code 0.12 seconds after the first, I'd recommend using a while loop and waiting for "waterfallGo" to be true.

avatar image vpg260 zblase · Apr 07, 2020 at 08:28 AM 0
Share

I just completely omitted the WaterfallTimer script and thus the waterfallgo value for testing purposes.

I'm still having the same issue.

The functionality I'm looking for is for the condition to execute if the object's frozen is set to false. As it stands it doesn't work on any of my objects, unless I add a breakpoint and manually click the serialized $$anonymous$$en value.

And even then it only works on one of my GameObjects, the second one bases it behavior on the first.

Perhaps some additional info will help: It concerns two waterfalls in an array. I'm telling them to only cascade if they aren't frozen. In my scenes I have a frozen waterfall and one unfrozen one.

The problems:

1 Once I run my code both my frozen and unfrozen waterfall start cascading.

2 Freezing them through code doesn't stop the code, only manually clicking the Serialized field ''$$anonymous$$en'' does.

3 Even the, only the first one works. Second object does whatever the first does.

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

128 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

Related Questions

Assign RectTransform to Array based on another Array 0 Answers

How to get all components that contain Color field 2 Answers

How to use a foreach loop to disable scripts on objects within a GameObject array? 1 Answer

Vector3.Lerp curving along three destinations 1 Answer

Array of objects - script only works on the first object in the array 2 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