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!
Answer by Socapex · Dec 28, 2016 at 11:29 PM
Keep a list of all those objects, for loop, bingo ;)
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!
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.
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?
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 ;)
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!