Calling prefabs by name or tag returns null.

Hey all,

So I have this code in a for loop.

string objCreated = "C" + t.ToString() + r.ToString() + d.ToString() + l.ToString();
				print("object created " + objCreated);
				GameObject currentObj = GameObject.FindGameObjectWithTag(objCreated);

				borderArray[x,z]= currentObj;

What is happening with the code that happens before this section, is it is checking tiles around the current x,z location in the array, if the tile above, below, left, right is a 0 or 1 for each tile and if there is no tile up down left or right it creates a 1 or 0.

My tile names are all 1100 or 0110 etc 0 means off 1 means on or open. So with the random 1 or 0 generates my tile name C1111. I have 16 combinations of tiles which are my prefabs and also called C1010 etc etc…

Thus I am entering the generated name into a string variable. I then want to call the actual prefab and enter it in the current x,z coords in my array[,].

The problem I have is I always get a Null value and thus no tile is chosen and no tile is entered in my array. If I manually type in the tile name, this will be entered.

What am I missing or misunderstanding with calling tags, or using .name ?

So, for each of the 16 tile types, you may want 5 or 10 or more copies of it? If so, there needs to be an Instantiate for each copy.

Are the 16 types in the scene, or are they real prefabs (in Project)? For real prefabs, Find won’t work. Find only finds created, in the scene, gameObjects. You generally pre-load them in public variables. Maybe in your case: int tileNum=t*8+r*4+d*2+l; Transform tilePF = TlleList[tileNum]; where TileList is an array of the 16 types.

You can pre-drag 1 of each tile type into the scene, and put them at -9999 or something. Then Find by name and Instantiate them. That seems like what your code is starting to do. But not as useful a trick – suppose you have a bullet that dies after 5 seconds.