How do I get each GameObject to mind its own business?

Hi Guys,

Pretty new to programming here… I have created a script to generate things that stack. Such as Trees or Towers. My plan is to put 1 base item down, it will stack X amt of mid sections on top of it, and place a topper on it when its done.

So far this works, but only if I have GameObject using the script at a time. If I try to add more GameObjects using the same script, they all just build off of 1 starting point on top of each other!!

How do I get these GameObjects to do their own thing? Have a look at my script. Thanks!

	public GameObject[] basePiece;
	public GameObject[] midPiece;
	public GameObject[] topPiece;
	
	public int _height = 1;
	
	public int maxHeight = 20;
	public int minHeight = 1;
	
	public bool isTree = false;
	public bool isTower = false;
	
	public float treeTilt = 0.0f;
	
	public GameObject curSpawnPoint;
	public GameObject curObject;

	void Awake () {
		
		curObject = this.gameObject;
		
		if(isTower)
			SpawnTower();
		
		if(isTree)
			SpawnTree();
	
	}
	
	public void SpawnTower(){
		if ( minHeight < 1 ){
			minHeight = 1;
		}
		
		if ( maxHeight > 20 ){
			maxHeight = 20;
		}
		
		_height = Random.Range(minHeight, maxHeight + 1);


		
		curSpawnPoint = GameObject.Find("spawnPoint");
		
		for (int cnt = 0; cnt < _height; cnt++){
			
			GameObject go = Instantiate(midPiece[Random.Range(0, midPiece.Length)],
										curSpawnPoint.transform.position,
										curSpawnPoint.transform.localRotation
										) as GameObject;
		
			go.transform.parent = curSpawnPoint.transform;
		
			curObject = go;
		
			curSpawnPoint = curObject.transform.Find("spawnPoint").gameObject;
		}
		
		GameObject goTop = Instantiate(topPiece[Random.Range(0, topPiece.Length)],
						curSpawnPoint.transform.position,
						curSpawnPoint.transform.localRotation
						) as GameObject;
	
		goTop.transform.parent = curSpawnPoint.transform;
	}

How sure are you that different gameObject are building on the same tower?

From your code (which I did not run and test yet), you are spawning the tower in one spawn point. (curSpawnPoint = GameObject.Find("spawnPoint");)

Is it possible that your code is working, but all the tower are built in the same spot, hence creating the illusion of “gameObject sticking into another gameObject’s business”?