Random Enemy 2D C#

Hey guys. I have a problem with my code. I want to make an enemy that it can show up everywhere and when I click that I get a poin, and the problem is when I run it the enemy doesn’t show up. I thing there is a problem with this RandomScript. Can you help me guys?

Here’s the script…

public class RandomScript : MonoBehaviour {

public GameObject[] bocor;
public GameObject[] bocorSedang;
public GameObject[] bocorBesar;
public Transform bocorK;
public Transform bocorS;
public Transform bocorB;
Transform bolong;

void SpawnBocor(){
	float x = Random.Range (5, 5);
	float y = Random.Range (5, 5);

	int R = Random.Range (-100, 100);
	switch (R) {
	case 0: bolong = Instantiate(bocorK) as Transform;
		break;
	case 1: bolong = Instantiate(bocorS) as Transform;
		break;
	case 2: bolong = Instantiate(bocorB) as Transform;
		break;
	}

	bolong.position = new Vector3 (x, y, 5);
}

void Start(){
	InvokeRepeating ("SpawnBocor", 1f, 4f);
}

}

You are generating random values in the range of -100 to 100, but you are only catching the case for 0, 1, and 2. This means each time ‘SpawnBocor’ is called, you only have a 1.5% chance that something will be instantiated, and you are calling it every 4 seconds…so with current values, on average you’ll get a new ‘bolong’ every few minutes. In addition, you set the position of the ‘bolong’ regardless of whether anything is created, which likely is resulting in a null reference exception. As a start, change line 13 to:

 int R = Random.Range(0, 3);

And if you use any wider values, you need to check to make sure ‘bolong’ is not null before setting the position on line 23.