[solved] creating an @editor script that adds components

hi!

first of all, i started using unity about two weeks ago tops, and i have been tackling down all kind of stuff with the mighty help of the chat channel and this forum (together with the good reference)

well, i have a scene with lots of cameras, that i might edit now and then in 3d studio max.

every time i import the cameras i will have to select them and add the components:
-camera
-gui layer
-skybox
-smooth lookat

then set the smooth lookat to look at the dummy named as the camera but ending in .Target

(asset name example: camera01.Target)

well. to do that with 3 or 4 cams might not be that much work, but i have more than 30. and animation changes or adding cameras will mean everything back again.

my plan was to do a script that runs trhough the adding component things, just creating 2 arrays, one with cameras and one with the targets.

once i add the components to the cams then the targets get set.

after a few hours, here it is:

notes:
-Malabar is the name of the company i work for
-my name is Martin Berisso
-and the skybox was always the same, so it didnt need to be set in this script, but in render settings.

// this script gets the "camaras" object that is the father to FBXimported cameras (camaras.fbx)
// upon import, those cameras are only gameObjects with transform.
// if a component "camera" is added, they look someplace not right, thats why the lookat script gets added

@MenuItem ("Component/Malabar/Cameras/Asign and Set Components")

static function Target_asign () {

	// all childs of the "camaras" object:
	var todas_las_camaras : Array= new Array();
	// filtered out the Dummies and targets:
	var camaras_main : Array = new Array();
	//only the camera´s targets (always contain ".Target" in the name):
	var camaras_target : Array = new Array();
	
	var cam_padre : Transform;
	cam_padre = GameObject.Find("camaras").transform;


	// fills the arrays:
	for (var child : Transform in cam_padre) {
		todas_las_camaras.Add (child);
	}
	for(i=0 ; i<todas_las_camaras.length; i++){
		if(!(todas_las_camaras[i].name.Contains("Dummy"))){

			if(todas_las_camaras[i].name.Contains(".Target")){
				camaras_target.Add (todas_las_camaras[i]);
			} else{
				camaras_main.Add (todas_las_camaras[i]);
			}
		}
	}
	
	// adds all the components needed. skybox will be set at render settings this time.
	for(i=0; i<camaras_main.length; i++){
		camaras_main[i].gameObject.AddComponent ("Camera");
		camaras_main[i].gameObject.AddComponent ("GUILayer");
		camaras_main[i].gameObject.AddComponent ("SmoothLookAt");
		
		var this_cam_lookat : SmoothLookAt;
		this_cam_lookat = camaras_main[i].gameObject.GetComponent (SmoothLookAt);
		this_cam_lookat.damping = 0;
		this_cam_lookat.smooth = false;
		this_cam_lookat.target = camaras_target[i];	//this is a transform array
	}

}

Hi, welcome to the forum!

There’s an example of how to attach an editor script to a menu item on this doc page.

You can iterate through all the children of an object using the transform property. There is an example of this right at the top of the Transform class doc page.

There is a string method called Contains that looks for a substring within a string.

Have a look at GameObject.AddComponent.

never thought it would be that simple XD

i had the idea of having to use special functions to get access to the assets while in editor mode, as a guide on which version (@scene or @asset) i had to use and so on…

thanks a lot for taking the time on answering that!
(i know that sometimes when the answer seems too obvious no one actually writes it down, so extra kuddos here!)

this lines throw an error BCE0020 on line 7:
(cam_padre = gameObject.Find(“camaras”);)

An instance of type ‘UnityEngine.Component’ is required to access non static member ‘gameObject’.

// Add menu named "Do Something" to the main menu
@MenuItem ("Component/Malabar/Cameras/Asign and Set Components")

static function Target_asign () {

	var cam_padre : GameObject;
	cam_padre = gameObject.Find("camaras");


	
	for (var child : GameObject in cam_padre) {
		print("child.name");
	}

}

what am i doing wrong here?

edit:

thanks to danielbrauer at the chat room, the mistake was to set gameObject.Find and not GameObject…

very easy lol, but would have never figured it out myself XD

corrected code:

@MenuItem ("Component/Malabar/Cameras/Asign and Set Components")

static function Target_asign () {

	var cam_padre : Transform;
	cam_padre = GameObject.Find("camaras").transform;

	for (var child : Transform in cam_padre) {
		print(child.name);
	}

}

ok!

it is done!

edited first post with a free script =P!

thanks to all!