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 /
  • Help Room /
avatar image
0
Question by iinot · Dec 26, 2016 at 04:12 PM · prefabvelocityfreezeclonezero

How to remove all velocity for all prefab/clone objects in the scene

Hi guys,

I've been looking through the forums for many examples to remove velocity from all objects in the scene. Currently, all my objects that have velocity are prefab clones.

The examples I've come by is only explaining how to remove velocity from one object at a time. I need all velocity from every single object to be removed in the scene to create the effect of everything frozen in place.

Does anyone know an efficient method in doing so, I do not want to timescale = 0f to stop it because I have a coroutine running so after 2 seconds it would change to game over scene. Or is the only option in zeroing out the velocity of every object one by one.

Thanks in advance!

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
0
Best Answer

Answer by Socapex · Dec 28, 2016 at 11:29 PM

Keep a list of all those objects, for loop, bingo ;)

Comment
Add comment · Show 6 · 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 iinot · Dec 29, 2016 at 02:18 AM 0
Share

Hi Socapex,

I understand I can do it in a loop but is there a way to just directly reference all prefabs because these objects are generated through a spawn point and it could have over 20 objects in the scene.

The only thing I can think of now is to find components by tag and then like you said store a list of the objects and then loop with a zero velocity function. However, it still feels quite inefficient. I was hoping there would be some sort of way to reference prefabs/clones. I did find an example where people can reference objects by tags in the hierachy not sure if that would work.

Please let me know what you think, thanks for helping out!

avatar image Socapex iinot · Dec 29, 2016 at 03:36 AM 1
Share

Hey no problem. 20 objects is really nothing, extremely tiny. You might start getting issues with a few thousands object loop (rendering those is another issue)

If the objects are generated by a spawn point, you can store them in that class at creation time. It is worth noting that if you have performance concearns, storing the actual component ins$$anonymous$$d of a GameObject will speed up the loop.

For example, upon creation

 // In the class definition.
 List <$$anonymous$$yScript> obj_list = new List<$$anonymous$$yScript>();
 
 // In a spawn loop somewhere near you.
 GameObject o = Instantiate<GameObject>(prefab);
 o.DoStuff;
 obj_list.Add(o.GetComponent<$$anonymous$$yScript>());

Later:

 foreach ($$anonymous$$yScript x in obj_list) {
     x.StopVelocity();
 }

I wouldn't worry about performance at all for now. Just keep on learning, some day you'll get a real bottleneck and then you can start investigating performance solution. Hope this helps.

avatar image iinot · Dec 29, 2016 at 04:43 AM 1
Share

Thanks for the detailed response. Just one more question what happens to the object if I destroy it after it has been added to the list? Does it disappear in the as well or does it remain as a reference in the list but will produce an error because the object does not exist?

avatar image Socapex iinot · Dec 29, 2016 at 05:19 AM 0
Share

It will be null in the list indeed. You can remove it when you destroy your objects obj_list.Remove(obj). If it isn't that clear, then you can always remove all null objects from the list before you traverse it.

edit: Oh and btw, you should mark your question as answered if it is ;)

avatar image iinot Socapex · Dec 29, 2016 at 06:50 AM 0
Share

Thanks! I've combined your approach and a different solution together. It was a lot easier just creating child objects and adding another tag to it and call FindGameObjectsWithTag. I used an array ins$$anonymous$$d of a list, I think it's the same outcome. However now I am stuck with this one, would be appreciated if you can help me out.

 GameObject[] objs;
         objs = GameObject.FindGameObjectsWithTag("clone");
         foreach(GameObject clone in objs) {
             GameObject parentObj = clone.transform.parent.gameObject;
             Rigidbody2D rb = parentObj.GetComponent<Rigidbody2D>();
             rb.velocity = Vector2.zero;
             rb.angularVelocity = Vector2.zero;
         }

Error is Cannot implicitly convert type 'UnityEngine.Vector2' to 'float'

Thanks again!

Show more comments

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

98 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

Related Questions

[Solved] Zero out Velocity before double jumping? 0 Answers

how the enemy spot the bomb with bomb prefab clone ? 0 Answers

Setting Continuous Collision Detection is breaking bouncy 2D physics 0 Answers

Instantiate a prefab and access sub-mesh material 0 Answers

Changing sprite alpha on all prefab clones 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