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 JJ_FX · Mar 15, 2019 at 10:33 AM · arraysforeach

Function executes only on 1 object

Hi,

I have two types of objects in my scene. GameObject[] Blockers and GameObject[] Meshes. If there is a Blocker object right on top of Mesh object I want Mesh object to change material. Blockers objects are moveable, so I want to execute this in Update().

But this function only works in Start() and in Update() only works on 1 object. Even though I checked, and for 2 Blockers, contition "occupied" returns true for 2 Meshes. But function SetMaterialsHere() executes only on 1 object.

So first question: why is that?

And second: is it a good way to change materials? Do I realy want this code to run every frame? Wont this slow down my game?

 void Action(GameObject[] Blockers, GameObject[] Meshes)
 {
     foreach (GameObject b in Blockers)
     {
         foreach (GameObject m in Meshes)
         {
             Vector3 positionToCheck = b.transform.position + Vector3.down;
             bool occupied = m.transform.position == positionToCheck;
             if (occupied)
             {
                 SetMaterialsHere();
             }
         }
     }
 }
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 xxmariofer · Mar 15, 2019 at 01:04 PM 0
Share

if occupied is true setmaterialshere has to be executed twice, whats the setmaterialshere method? have you logged in and out of the if something for knowing how many times is true?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JJ_FX · Mar 15, 2019 at 01:28 PM

Ok I have found the problem. This was not the whole code. Thats whats going on:

              if (occupied)
              {
                  SetMaterialsHere(Material X);
              } else {
                  SetMaterialsHere(Material Y);
              }

So besically my material is set to Material X, but then when iterating over next Blocker object is overwrites the material back to Material Y...

So the proble is foreach inside foreach.... How to deal with that?

Comment
Add comment · Show 2 · 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 xxmariofer · Mar 15, 2019 at 01:33 PM 1
Share

set all the materials to Y first (outside those foreach) and inside the foreach only assign x if its occupied

avatar image JJ_FX xxmariofer · Mar 15, 2019 at 01:54 PM 0
Share

Omg ofcourse XD. Thank you so much!

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

104 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

Related Questions

All objects in an array not working properly 1 Answer

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

Get child position from last gameobject in array 2 Answers

Array problems 3 Answers

How to find x item in list? 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