Hey guys, im trying to make a nice simple script for fishing =) this is one of three scripts i have and for some reason i get a warning saying “ArgumentExecption: RandomRangeInt can only be called from the main thread”. Im not 100% sure why this is saying this becuase i’ve used it before a long time ago :S, also when fishing it seems to always do it for 10 seconds (Which is the second int in the var).
static var CurLoc = 1;
static var IsFishing = false;
var FishFilter = false;
var FishTimer = Random.Range(3,10);
var FLevel = 1;
function Update () {
if(IsFishing FishFilter == false) {
Fish();
FishFilter = true;
print("starting fishing");
}
}
function Fish () {
yield WaitForSeconds (FishTimer);
IsFishing = false;
print("fish caught");
yield WaitForSeconds (1);
FishFilter = false;
print("clearning timer");
}
anyone know? thanks 
var FishTimer : int;
function Awake()
{
this.FishTimer = Random.Range(3,10);
}
thanks for the reply, but i have no idea what to do with this.fishtimer to use it in a yeild :S
i tried:
yield WaitForSeconds.FishTimer = Random.Range(3,10);
yield WaitForSeconds (Random.Range(3,10);
and
static var CurLoc = 1;
static var IsFishing = false;
var FishFilter = false;
var FishTimer : int;
var FLevel = 1;
function Awake () {
FishTimer = Random.Range(3,10);
}
function Update () {
if(IsFishing FishFilter == false) {
Fish();
FishFilter = true;
print("starting fishing");
}
}
function Fish () {
yield WaitForSeconds (FishTimer);
IsFishing = false;
print("fish caught");
yield WaitForSeconds (1);
FishFilter = false;
print("clearning timer");
}
after checking i no longer get the warning, however it only seems to count to 3 :S
Edit: after relising im stupid XD i understnad it now, i thought everytime the variable was called it would assign a new and random number, but it doesn’t it declares the number in awake and thats why its counting to only 3!
fixed Version :
static var CurLoc = 1;
static var IsFishing = false;
var FishFilter = false;
var FishTimer : int;
var FLevel = 1;
function Start () {
FishTimer = Random.Range(3,10);
}
function Update () {
if(IsFishing FishFilter == false) {
Fish();
FishFilter = true;
print("starting fishing");
}
}
function Fish () {
yield WaitForSeconds (FishTimer);
IsFishing = false;
print("fish caught");
yield WaitForSeconds (1);
FishFilter = false;
print("clearning timer");
RunNewTimer ();
}
function RunNewTimer () {
FishTimer = Random.Range(3,10);
}
I ran into a similar problem with Random.Range not functioning in a separate thread (an asynchronous method called with BeginInvoke). I had to use System.Random but I made a wrapper to make it function like the Unity Random object so I wouldn’t have to muck about in my code.
public class MyRandom
{
private System.Random rnd;
public MyRandom()
{
rnd = new System.Random();
}
public int Range(int min, int max)
{
return (rnd.Next(min, max));
}
public float Range(float min, float max)
{
if (min > max) {
return min;
} else {
return ((float)rnd.NextDouble() * (max - min)) + min;
}
}
}
The downside is that you have to instantiate the class in order to ensure thread-safety.