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 UnityJP · Jun 23, 2011 at 07:24 PM · gameobjectmovingmotion

Check if a set of objects is moving

Hi,

I'm trying to define a function that will check if a certain set of objects is moving. The problem is that Unity will crash on me during the execution of this code. Not sure if I'm doing something wrong in the code or perhaps there is an easier method than what I'm trying to use. The function is called when a certain type of object is destroyed if that makes a difference

 bool partsAreMoving = true;
 while(partsAreMoving)
 {
   Debug.Log("Loop Start");
   partsAreMoving = false;
   GameObject[] machineParts = GameObject.FindGameObjectsWithTag("MachinePart");
   foreach(GameObject part in machineParts)
   {
     if(part.rigidbody == null)
       continue;
     if(part.rigidbody.velocity.magnitude > 0) //i.e. moving
     {
       Debug.Log("Moving " + part.rigidbody.velocity.magnitude);
       partsAreMoving = true; //Crashes if I don't comment this out
     }
     else
     {
       Debug.Log("Not moving");
     }
   }
   Debug.Log("Loop end");
 }

This will crash Unity when it hits it (I won't even get the "Loop Start" in the log) but if I comment out the line that sets partsAreMoving to true in the foreach loop it will work fine. What am I doing wrong? Is this too intense a calculation?

Thanks in advance

Comment
Add comment · Show 2
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 theot3 · Jun 23, 2011 at 07:52 PM 0
Share

how many game objects are we talking about?

avatar image UnityJP · Jun 23, 2011 at 08:47 PM 0
Share

Total in the scene? It'll depend but probably around 50 That I care about? Like 10-15

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DaveA · Jun 23, 2011 at 07:57 PM

  1. You should do the FindGameObjectsWithTag outside of the loop as it is an expensive call. If these things create/destroy you may want to manage a list of them, but in any case

  2. You set partsAreMoving to true and you loop on that fact. So if anything is moving, this loop will never exit, what looks like a crash is an infinite loop. You might want to yield or something, but I think it would be better to use Update to scan the list once and move on.

  3. For good measure, part.rigidbody.velocity.magnitude > 0.0 or 0f to make sure it's not using integer math

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 UnityJP · Jun 23, 2011 at 08:31 PM 0
Share

Would moving this to an Update() or FixedUpdate() help with the expensive FindGameObjectsWithTag call or do you still think it would be beneficial to have a managed list?

avatar image UnityJP · Jun 23, 2011 at 08:56 PM 0
Share

Thanks this got me going in the right direction

avatar image
0

Answer by UnityJP · Jun 23, 2011 at 08:56 PM

Using the stuff that DaveA suggested I've got this working now using the following (excluding the managed list, haven't done that yet)

 bool checkMachine = false;

 public void reset()
 {
   GoToMachine();
   checkMachine = true;
 }

 private void LateUpdate()
 {
   ...

   if(checkMachine)
     CheckIfPartsAreMoving();
 }

 private void CheckIfPartsAreMoving()
 {
   bool moving = false;
   GameObject[] machineParts = GameObject.FindGameObjectsWithTag("MachinePart");
   foreach(GameObject part in machineParts)
   {
     if(part.rigidbody == null)
       continue;
     if(part.rigidbody.velocity.magnitude > .01f)
       moving = true;
   }

   if(!moving)
     GoToStart();
 }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Character moving weirdly 1 Answer

Moving a GameObject freezes Unity (Possible Infinite Loop) 2 Answers

Moving objects in sceneview with arrow keys? 0 Answers

Animation Translation 0 Answers

Control the unscripted GameObject 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