How to set a component's property on multiples objects

Hello there,

I am a noobie on unity 2.5 on windows.

I have a little architecture 3D scene which I made a layer “collisions” containing all the “proxy” objects for collisions.

They need to be invisible and colliders.

I saw we couldn’t select multiple objects at once in the 3d viewport.

So I’m selecting all my collisions objects in the hierarchy to remove their “Mesh rendererer” property.
Only works on the first selected.

How to set this kind of property to all objects at once ? anything to script here ? (which would be nice too since i’m familiar with js, but I just started using unity !)

Thank you very much !
SMaX

You can certainly script this using Editor scripts.
Check out ScriptableWizard to get an idea of how to get started. Check out other editor classes and search the forum and wiki for editor scripts.
Make sure you put these scripts in a folder called “Editor” under your “Assets” folder.

I took a quick look into this, looks nice to be able to customize our editor’s functionnalities.

Just to get a little kicker help, what would be the code lines or class to use if I want let’s say to set the mesh renderer property to none on all the objects in my scene named “collisions_*” (where * is a wildcard) ?

I think i’ll get into this part quite heavily since the unity editor looks very basic.
Adding our own buttons linked to editor scripts would be great.

Thanks again !

I don’t think that searching by name is quite that fun or effective. You make one mistake when typing the name, or miss one name and you’re begging for errors. But if that is what you want you can try:

for (var currGO: GameObject in FindObjectsOfType(GameObject))
{
	// Check it's name...
	if (currGO.name.IndexOf("collisions_") == 0)
	{
		Debug.Log("Found: " + currGO.name);
		
		// Remove the mesh renderer component
		Destroy(currGO.meshRenderer);
	}
}

What I usually do in editor scripts is use the selection… If you select game objects in your scene, you can access that selection as an array from inside your editor script:

var selection: Object[];
selection = Selection.objects;

foreach (var currGO: GameObject in selection)
{
	// Remove the mesh renderer component
	Destroy(currGO.meshRenderer);
	
	// Create a NEW rigidbody component just for kicks
	currGO.AddComponent(MeshRenderer);
}

Check out Selection

If all your collision objects share the same material, a quick way to hide them is to set the material to Transparent, make sure you have alpha at 0.

Hi,

Thank you for the code (i prefered doing that in js rather than c# though)

I just coded a toggle shortcut to hide and show objects, i think it can be very convenient.

Here is my small piece of working code:

// Add menu item named "Toggle Visible-Invisible" to GameObject menu
@MenuItem ("GameObject/Toggle Visible-Invisible %h")

static function ToggleVisibleState() {
    for (var currentobj : Transform in Selection.transforms)
		if (currentobj.gameObject.renderer.enabled)
		currentobj.gameObject.renderer.enabled = false;
		else
		currentobj.gameObject.renderer.enabled = true;
}

// The menu item will be disabled if no transform is selected.
@MenuItem ("GameObject/Toggle Visible-Invisible %h", true)

static function ValidateToggleVisibleState() : boolean {
    return Selection.activeTransform;
}

Also when i do

currentobj.gameObject.AddComponent(MeshRenderer);

it works fine in adding a MeshRenderer to the selection, but when i write
currentobj.gameObject.Destroy(MeshRenderer); it says [quote]
No appropriate version of ‘UnityEngine.Object.Destroy’ for the argument list ‘(System.Type)’ was found.
[/quote]

Maybe I’m missing something here ?

Thanks anyway for this wonderful forums.

SMaX

Hey, glad to see you catch on quickly.

The reason you receive the error is because Destroy is expecting to receive an object (or a component, but either way an actual reference to something that inherits from the Object class) as a parameter…
In order to destroy the mesh renderer component, you first need to get a reference to it.
So either use Destroy(currObj.gameObject.GetComponent(MeshRenderer)), Destroy(currObj.GetComponent(MeshRenderer)) or Destroy(currObj.renderer).
When you add a component you need to give it a type to create. When you delete it, you need to give it a reference.
You could, theoretically have many instances of the same script or component on the same gameObject. How will the script know which one to destroy without a direct reference? :slight_smile: