ArgumentException: RandomRangeInt can only be called from the main thread error, please help

Hello,

I really need help on what this error message is;

ArgumentException: RandomRangeInt can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Random.Range (Int32 min, Int32 max) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineRandom.gen.cs:30)
HackDone…cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for HackDone
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
HackSpawn:Awake() (at Assets/HackSpawn.cs:16)

I get this when a certain level loads when testing my game in play mode. In the level that loads I have a empty game object that has a script which has an array of spawn points and in the awake function it spawns an item at one of those spawn points. (the item has a trigger collider which adds it to a counter on the player when the player enters).

If I have my scene as this level and hit play then I do not get any errors and the spawned items can be ‘collected’ correctly by the player. But if I play the previous level in the build first, and hit the end point of that level to load this one then that’s when I get this error message. The level still plays and the items still spawn, but the trigger collider on that item no longer works and this message pops up every time the player enters it.

I’m stumped as to what this can be, the scripts for spawning should be ok as I’ve used essentially the same script to spawn enemies etc in other level which do not generate any errors. Please help as its driving me nuts.

Possible relevant scripts below if it helps.

HackSpawn script which spawns the hackBox gameobject at one of an array of spawn points.

using UnityEngine;
using System.Collections;


	public class HackSpawn: MonoBehaviour
	{
		public GameObject hackBox;                        
		public Transform[] hackPoints;         
		
		
		void Awake ()
		{
			int hackPointIndex = Random.Range (0, hackPoints.Length);
			
			
			Instantiate (hackBox, hackPoints[hackPointIndex].position, hackPoints[hackPointIndex].rotation);

		}
	}

And this script below is the one on the hackBox object with a trigger collider for the player to collect

using UnityEngine;
using System.Collections;

public class HackDone : MonoBehaviour {

	public static int scoreValue = Random.Range (1000, 1500);
	public static int hackValue = 1;
	//public GameObject effect;
	
	
	void OnTriggerEnter (Collider other)
	{
		if (other.tag == ("Player"))
		{
			

			//Instantiate (effect, transform.position,transform.rotation);
			ScoreManager.score += scoreValue;
			HackManager.hack += hackValue;
			
			Destroy (gameObject);
			
			

			
			
		}
	}
}

public static int scoreValue = Random.Range (1000, 1500);

You can’t use Random.Range as an initializer for a static variable. Add an Awake or Start method and initialize scoreValue there. Or if there isn’t a compelling reason for it to be static, remove static.

I have simply moved my variable with Random.Range from the main class to inside of the function that was using that is using the variable and I didn’t get the error anymore. It can not be static though inside the function.

Although when I was getting this error I could still build it and use it inside the unity with the error showed in the console. Everything is running fine.