Getting Child GameObjects

Hi folks,

I’m working on a script to populate my scene with random wall decorations, placed where I’ve put locators. Going fine, but I want to limit the locator objects found to only the children of the object the script is applied to. So the groundfloor object will only populate it’s own locators, not those of the firstfloor.

If anyone knows a way to sort this out (namely the line with the rant…) I’d be really appreciative!

Cheers,

  • Jon
// List of possible wall decorations
var wallhangings = new Array();
wallhangings[0] = ("tapestry");
wallhangings[1] = ("mirror");
wallhangings[2] = ("picture");

var picturecount : int = 3;
	

for(var i : int = 1; i <= picturecount; i++) {

	// Get Random Decoration
	var chooseme : String = wallhangings[Random.Range(0, wallhangings.length)];
	var wallpiece : GameObject = GameObject.Find(chooseme);

	// Get Hanging Location
	var nailname : String = "locator" + i;
	var thisnail : GameObject = GameObject.Find(nailname); //CHILDREN ONLY?!? ARGLEBLARGLEGRR

	// Hang a Decoration at Location
	wallpiece.tag = thisnail.tag;
	Instantiate(wallpiece, thisnail.transform.position, Quaternion.identity);

	// What's Where
	Debug.Log (wallpiece.name + thisnail.transform.position);
}

Haven’t tried it lately, but I always used to use this:

foreach (Transform child in transform){}

GameObject.Find really is the wrong tool in this case. Of course another alternative would be to create tag groups and do a find based on tags.

That would select everything in the group though, wouldn’t it?

My problem is that all the locators have been placed in Maya so, as well as the locators in the group, there’s also all the level geometry. Would grouping the locators in Maya before importing them work? Thus letting me search only within that group? Would prefer not to, but if that’s what it takes…

I can’t use tags (unless there’s a way to set multiple tags on one object? That would be nice!) since they’re all tagged for other things already.

Cheers,

  • Jon

(aka simplex409. Why I have two usernames I really don’t know.)

Got this working now so thought I pop the results up here, to show how I did it in case anyone else wants to do the same thing.

function Start () {

	// List of possible Wall decorations
	var wallhangings = new Array();
	wallhangings[0] = ("tapestry");
	wallhangings[1] = ("mirror");
	wallhangings[2] = ("picture");
	
	var floorcount : int = 1; // To be set externally
	var picturecount : int = 3; // To be set externally
	
	// Populate the Walls
	for (var j : int = 0; j <=floorcount; j++ ) {
		for(var i : int = 1; i <= picturecount; i++) {
		
			// Get Random Decoration
			var chooseme : String = wallhangings[Random.Range(0, wallhangings.length)];
			var wallpiece : GameObject = GameObject.Find(chooseme);
			
			// Get Hanging Location
			var nailname : String = j + "floor/locator" + i;
			var thisnail : GameObject = GameObject.Find(nailname);
			
			// Hang a Decoration at Point
			wallpiece.tag = thisnail.tag;
			Instantiate(wallpiece, thisnail.transform.position, Quaternion.identity);
		}
	}
	
}

Goes through the floors named 0floor, 1floor, etc. and populates all the locators with whatever you set in the array.

Am highly chuffed at getting this working! Now to expand it so that the entire house is built dynamically from a big selection of rooms!

  • Jon