Bad accessing to an array - TD game

Hey guys I am really tired of this error, I dont really know what I am doing wrong.
I did set up everything correct, like assign towers to script. But the problem is in the script, I cant really figure out how to access towers array. I want to list towers in game from my array when building, but it gives me the error: NullReferenceException: Object reference not set to an instance of an object So can someone help me please?

using UnityEngine;
using System.Collections;

public class Slot : MonoBehaviour {
	public GUISkin skin = null;
	
	bool gui = false;
	
	// Tower prefab
	public Tower[] towers = null;


	void OnGUI() {    
		if (gui) {
			GUI.skin = skin;

			// get 3d position on screen        
			Vector3 v = Camera.main.WorldToScreenPoint(transform.position);
			
			// convert to gui coordinates
			v = new Vector2(v.x, Screen.height - v.y); 
			
			// creation menu for tower
			int width = 200;
			int height = 40;
			Rect r = new Rect(v.x - width / 2, v.y - height / 2, width, height);

			for (int i = 0; i < towers.Length; i++) {
			GUI.contentColor = (Player.gold >= towers*.buildPrice ? Color.green : Color.red);*

GUI.Box(r, “Postavit " + towers_.name + “(” + towers*.buildPrice + " zlata)”);*_

* // mouse not down anymore and mouse over the box? then build the tower*
* if (Event.current.type == EventType.MouseUp &&*
* r.Contains(Event.current.mousePosition) &&*
_ Player.gold >= towers*.buildPrice) {
// decrease gold*

Player.gold -= towers*.buildPrice;*_

* // instantiate*
_ Instantiate(towers*, transform.position, Quaternion.identity);*_

* // disable gameobject*
* gameObject.SetActive(false);*
* }*
* }*
* }*
* }*

* public void OnMouseDown() {*
* gui = true;*
* }*

* public void OnMouseUp() {*
* gui = false;*
* }*
}
It gives me an error on 18th line which is Vector3 v = Camera.main.WorldToScreenPoint(transform.position); Its not definitely that coz it worked with it when i had script only for one tower without any arrays… Please help :slight_smile:

If it really is on line 18, it means you created a new camera and deleted the main one, or you untagged MainCamera from the main camera, or you disabled the main camera. If you did any of those, problem solved (I would just double check anyway even if you don’t think you did those, sometimes keystrokes with wrong focus can mess things up).