Player spawn point not working.

I’m trying to make a 2D top down RPG style game and it was going fine until I tried to add a spawn point. When I added the 3-4 lines of code that i used for the spawn point, i tested the game and noticed it didn’t spawn me at the spawn point. When I went to where the spawn point was supposed to be, I noticed that the level stopped loading once it reached the spawn point.

Here is the level C# Script, the lines of code for the spawn point will be surrounded by “!”.

using UnityEngine;
using System.Collections;

public class Level : MonoBehaviour {

	private int levelWidth;
	private int levelHeight;

	public Transform grassTile;
	public Transform stoneBrickTile;

	private Color[] tileColors;

	public Color grassColor;
	public Color stoneBrickColor;
	!public Color spawnPointColor;!

	public Texture2D levelTexture;

	public Entity player;

	void Start () {
		levelWidth = levelTexture.width;
		levelHeight = levelTexture.height;
		loadLevel ();
	}

	void Update () {
	
	}

	void loadLevel()
	{
		tileColors = new Color[levelWidth * levelHeight];
		tileColors = levelTexture.GetPixels();

		for(int y = 0; y < levelHeight; y++)
		{
			for(int x = 0; x < levelWidth; x++)
			{
				if(tileColors[x+y*levelWidth] == grassColor)
				{
					Instantiate(grassTile, new Vector3(x, y), Quaternion.identity);
				}
				if(tileColors[x+y*levelWidth] == stoneBrickColor)
				{
					Instantiate(stoneBrickTile, new Vector3(x, y), Quaternion.identity);
				}
				!if(tileColors[x+y*levelWidth] == spawnPointColor)
				{
					Instantiate(grassTile, new Vector3(x, y), Quaternion.identity);
					Vector2 pos = new Vector2(x, y);
					player.transform.position = pos;
				}!
			}
		}
	}
}

Any idea what happened?

Do you have the Unity Debug Console visible with errors enabled (icon on the far right side at top)? Are you not getting a null reference exception on the “player.transform.position = pos” line? It’s the only thing I can think of that would cause it to stop running this code. So just be sure you set player to a valid value in the Inspector for this script.

Likely the grassTile is set already since you’re using it above this code, but if by chance this is the first time you instantiate a grass tile, also check that it is assigned in the inspector. You should be able to simply step in and through this code using MonoDevelop too. Just open mono, click the Play button (really attach) in the upper left corner, and attach to the Unity Editor. Then left click in the gutter just to the left of the Instantiate(grassTile… call. Now hit play in unity. When it stops in Mono here, hit the “Step Over” button in Mono for each line of code until it crashes.

Alternatively, you can just attach Mono develop and not add any breakpoints, and if any exceptions are thrown, Mono will stop on that line of code.