"Dynamic" variables with c#

I’m aware variables can’t be declared at run time but I’m hoping to be able to reference one that’s already compiled. Let me explain the situation:

I’m generating a 2.5d side scrolling level. I start with a straight piece (a tile). I’ve set up an array saying which pieces are compatible with the tile (another straight, or an end cap).

This is what I have right now and I think it will explain much better than I could with words:

void StageBuilder()
		for(int i = 0; i < tileCount; i++)
			Transform thisTile = nextTile[Random.Range (0,nextTile.Length)];
			Transform startTile = Instantiate(thisTile, tileStart,  Quaternion.Euler(270f, 0f, 0f)) as Transform;
			tileStart.x -= 6f;
			string hackProgramming = thisTile.name+"Arr";
			nextTile = hackProgramming as Transform;


The resulting string would be the name of the array with compatible pieces. From there, I’d be able to pick another tile randomly and repeat the process.

I’ve read through and I think I understand what you want. Here are the steps I would take:

  • In the prefab for each Tile, you give it a component
  • Within that component have an array of Transforms
  • To that array, add each Tile prefab which can follow that prefab.
  • In StageBuilder, use GetComponent() to get the component of the Tile you’ve just made.
  • Set nextTile(s) to the array of Transforms in that component.

I hope that helps. I’d write it all out as code, but would take me far longer and I’m just finishing up for this evening.