Find a mesh in a scene and rotate it

Hi,

I need to detect all the meshes of a certain name in a scene (up to 16 of them) and rotate them all by 270 degrees.

Script starts > detect ‘AI Mesh’ in scene > Rotate all detected items by 270 degrees > End.

I’ve got no idea where to start, any help will be appreciated

Thanks everyone!

You should start by reading the unity documentation so you get the most out of it, that’s a very basic question, I think they’ve got your exact problem explained under “common operations”. (sounds like some sort of unity introduction course question, they probably took they’re question from the docs)

Solution will involve something like

GameObject.FindGameObjectsWithTag("bla") 

which will give you a list of objects with tag bla, then you’ll have to run to the list and set the desired rotation. Something along the lines of:

object.transform.rotation += Quaternion.Euler(0,270,0);

Note that FindGameObjectsWithTag is based on the tag of the object, not the actual name. If you need to base it on the actual name, GameObject.Find(“bla”) finds an object with the name “bla”, but returns only one object as far as I know. So you’d have to write a workaround(which shouldn’t be all that hard). As far as I know there is no function like GameObject.FindAll(“bla”), probably because it’s considered bad practice to name different Objects the same(and searching for strings is inefficient, so tags probably make more sense).

Well, now, a Mesh is a different thing than a Transform.

Do you want to spin all the vertices, but keep the same facing for the actual object? You can access the .vertices array of one object’s sharedMesh and use rotational math (which I am terrible at) to spin the vertices around whatever centerpoint you want.

If you need to find Transforms by the Mesh they use, you can do this:

var m : MeshRenderer;
for ( var t : Transform in GameObject.FindObjectsOfType(Transform) ) {
  m = t.GetComponent(MeshRenderer);
  if ( m ) {
    if ( m.mesh.name == targetMeshName ) {
      t.Rotate( Vector3(0,270,0) )
    }
  }
}

It might be better, though, to maintain that list of Transforms throughout the game (by adding/subtracting to a ‘global’ list whenever one of those objects is created/destroyed) rather than recreating it (depending on how often that gets run / how many objects you have, that could create a lot of slowdown).

Don’t forget about the Scripting Reference at Unity - Scripting API:, where you can look up any of those things I wrote. :slight_smile:

You can find only one object by name: any other objects with the same name will be ignored. But there’s a simple solution: use tags instead of names - you can get all objects with the specified tag in an array at Start, and rotate them when needed:

private var aiMeshes: GameObject[];

function Start(){ // get all objects tagged AIMesh
  aiMeshes = GameObject.FindGameObjectsWithTag("AIMesh");
}

// call this function to rotate

function RotateEverybody(){
  for (var mesh: GameObject in aiMeshes){ 
    mesh.transform.Rotate(0, 270, 0); // rotate 270 degrees about Y
  }
}

For this to work, however, you must tag all “AI Mesh” objects as “AIMesh”. Since this is a custom tag, you must first register it: click the Layer button above the Inspector, then click the field Tags and add it.