- Home /
edit values for multiple objects concurrently
Lads, I'm creating a scene with a lot of box colliders. I'd like to be able to tweak certain properties like 'Mass' for all colliders at the same time. Currently I have to open each box collider and tweak the values one at a time, and this is time consuming... any suggestions?...
Thx,
Stef
Editor script, or at runtime? In any case, there's a method that is common to both, which I'll outline below.
Answer by syclamoth · Nov 29, 2011 at 05:36 AM
The most straightforward way, in the editor, is to just use prefabs for the boxes! If you create and instantiate a single prefab, as long as you don't change the 'mass' value on the instantiated box you can change the value on the prefab in the project view (or by clicking apply in the scene view), and all the instances in the scene will automatically update to reflect that change.
If, for some reason, this won't work for you (for example, if you have to do this at runtime), you can do it using Unity's tagging system!
Whenever you create one of these boxes, give it a tag corresponding to a 'type' of box (this allows you to have several independently-controlled boxkinds). Then, when you want to change the mass of all the boxes, use this function-
public void ChangeMassWithTag(string tag, float newMass)
{
foreach(GameObject obj in GameObject.FindGameObjectsWithTag(tag))
{
Rigidbody objBody = obj.rigidbody;
// Make sure the rigidbody actually exists!
if(objBody != null)
{
objBody.mass = newMass;
}
}
}
This will work both at runtime, and in the inspector- just call it like this:
// to change the masses of 'barrel' objects to 5.6
ChangeMassWithTag("barrel", 5.6f);
Your answer
![](https://koobas.hobune.stream/wayback/20220613060024im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change objects in scene 3 Answers
Multiple Materials on Multiple Objects -runtime- 1 Answer
Multiple Button Modifications 2 Answers