Instatiate is only working when it is wrong

I’ve been working on this problem for a while. I am trying to instantiate glowing squares on my board to highlight the possible paths you can go. My search algorithm was incorrect at first and it was highlighting every square on the board. When I seperated the search and instantiation functions, it still created too many squares. I fixed my algorithm and I’m getting the right amount of squares to create and I references to all of them, but now they aren’t appearing in game even though I changed nothing in the instantiation function. Any ideas?

My instantiation function:

public void drawSquares(){
		float x;
		float y = 1f;
		float z;
		for(int i = 0; i < range.Count; i++)
		{
			x = range*.transform.position.x;*

_ z = range*.transform.position.z;_
_
Instantiate (square, new Vector3(x,y,z), Quaternion.identity);_
_
}_
_
}*_
where range is a List of scripts that represent my tiles. I’ve double and triple checked; range is correct. Square is defined above as a gameObject. it’s just a reference to my prefab that I dragged and dropped into the script via the editor.

You should: * Switch to scene view when the game is running * Highlight one of the instantiated squares in the hierarchy * Press F to focus it in the scene view * Check the location you have instantiated and verify that you can see the object

That's the issue though; Unity calls the command, I've seen it in the debugger in mono and even though it makes the call and nothing is output in the console, no squares are instantiated. Nothing shows up in the hierarchy.

Confirm the method is being called, add a Debug : public void drawSquares(){ Debug.Log( "drawSquares was called" ); // rest of code

did you put the prefab in the slot for the square gameobject on the script? your "square" is a prefab, right? did it show any error message?

They were both prefabs. No error messages, and I was going into the drawsquares function with the debugger. Since I couldn't figure out how to fix it, I moved on to other stuff in the game, and even though I didn't touch that script or anything it works upon, the problem magically went away.

1 Answer

1

I don’t know why or how, but the problem went away.