- Home /
Weird Code Error Message
Here is the code I'm trying to compile:
var scare : Transform;
//create a random number between 1-0
private var randomNumber = Random.Range;
//activate trigger
function onTriggerEnter (other:Collider) {
if (other.gameObject.tag == "Player"){
//create weighted number
var chance = randomNumber * Screwedometer.screwedLevel;
var decider = chance / 15000;
if (Random.Range <= decider){
///stufftodogoeshere///
}
}
}
However, it doesn't work and the error it is giving me is "Assets/Scripts/Trigger.js(13,43): BCE0051: Operator '*' cannot be used with a left hand side of type 'function(float, float): float' and a right hand side of type 'float'. "
Any ideas?
Try putting your private var randomNumber = Random.Range; within the trigger function. I don't think Unity likes making calculations outside functions (I had a similar problem once declaring an array then trying to populate it outside a function). Just a suggestion =]
As Jordan stated and I overlooked, I think you want Random.value
Answer by Landern · Jan 08, 2013 at 02:40 PM
Isn't Range a function?
The usage should be a min and a max.
private var randomNumber = Random.Range(0, 1);
Same thing in your if statement.
agh, you're right. I read Random.value ins$$anonymous$$d of Random.Range !
I agree with alucardj, Random.value will give the value between 0.0-1.0 and is most likely what was intended.
Thanks for the help, would that return a float value, like 0.235? Or does it just return either 1 or 0?
Returns float value =]
API : http://docs.unity3d.com/Documentation/ScriptReference/Random-value.html
Brilliant, it works perfectly. $$anonymous$$arking you as the answer, thanks again!
Answer by tosi · Jan 08, 2013 at 02:47 PM
Random.Range is a function call. You should write: randomNumber = Random.Range(0,1);
Stuck in the moderation que, hey! I shall upvote you too (upvoted Jordan already) =]