- Home /
Instantiating Dots
#pragma strict
var dotcyan : GameObject;
var dotred : GameObject;
var dotgreen : GameObject;
var dotyellow : GameObject;
var dotorange : GameObject;
function awake () {
var Mathf.Clamp01 : float;
if (Mathf.Clamp01 < .2) {
Instantiate (dotcyan,0,0);
} else if (.2 <= Mathf.Clamp01 < .4) {
Instantiate (dotred,0,0);
} else if (.4 <= Mathf.Clamp01 < .6) {
Instantiate (dotgreen,0,0);
} else if (.6 <= Mathf.Clamp01 < .8) {
Instantiate (dotyellow,0,0);
} else if (.8 <= Mathf.Clamp01 < 1) {
Instantiate (dotorange,0,0);
}
}
I am trying to randomly instantiate one of the five prefabs I have created. I am getting error BCE0043. Would Mathf.Clamp01 be the appropriate method? Once this works, I want to repeat it with different transforms. Thanks to anyone who helps.
You really should use an array, not a bunch of separate variables.
Answer by getyour411 · Apr 22, 2014 at 12:25 AM
Take a look at the UA/Google examples of using Mathf.Clamp01 and Random()
http://forum.unity3d.com/threads/82050-Mathf-Clamp01-always-return-1
https://docs.unity3d.com/Documentation/ScriptReference/Random-value.html
Thank you. I changed my code to reflect a correct usage with $$anonymous$$athf.Clamp01. However, I still quite can't get the code to run. I'm getting these three types of errors: (13,22): BCE0051: Operator '<' cannot be used with a left hand side of type 'boolean' and a right hand side of type 'float', (14,15): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(String, int, int)' was found, (10,25): BCE0022: Cannot convert 'function(float): float' to 'float'.
The current code is as follows
#pragma strict
var dotcyan : GameObject;
var dotred : GameObject;
var dotgreen : GameObject;
var dotyellow : GameObject;
var dotorange : GameObject;
function awake () {
var i : float = $$anonymous$$athf.Clamp01;
if (i < .2) {
Instantiate ("dotcyan",0,0);
} else if (.2 <= i < .4) {
Instantiate ("dotred",0,0);
} else if (.4 <= i < .6) {
Instantiate ("dotgreen",0,0);
} else if (.6 <= i < .8) {
Instantiate ("dotyellow",0,0);
} else if (.8 <= i < 1) {
Instantiate ("dotorange",0,0);
}
Thank you. I changed my code to reflect a correct usage with $$anonymous$$athf.Clamp01. However, I still quite can't get the code to run. I'm getting these three types of errors: (13,22): BCE0051: Operator '<' cannot be used with a left hand side of type 'boolean' and a right hand side of type 'float', (14,15): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(String, int, int)' was found, (10,25): BCE0022: Cannot convert 'function(float): float' to 'float'.
The current code is as follows
#pragma strict
var dotcyan : GameObject;
var dotred : GameObject;
var dotgreen : GameObject;
var dotyellow : GameObject;
var dotorange : GameObject;
function awake () {
var i : float = $$anonymous$$athf.Clamp01;
if (i < .2) {
Instantiate ("dotcyan",0,0);
} else if (.2 <= i < .4) {
Instantiate ("dotred",0,0);
} else if (.4 <= i < .6) {
Instantiate ("dotgreen",0,0);
} else if (.6 <= i < .8) {
Instantiate ("dotyellow",0,0);
} else if (.8 <= i < 1) {
Instantiate ("dotorange",0,0);
}
Change this
var i : float = $$anonymous$$athf.Clamp01;
to this
var i : float = Random.value;
and capitalize Awake
Thank you getyour411. You have been a big help. I now realize that $$anonymous$$athf.Clamp01 "clamps" a variable, and doesn't assign it a value. I made the changes you suggested but I am still getting errors BCE0051 and BCE0023. Here is the current code
#pragma strict
var dotcyan : GameObject;
var dotred : GameObject;
var dotgreen : GameObject;
var dotyellow : GameObject;
var dotorange : GameObject;
function Awake () {
var i : float = Random.value;
if (i < 0.2) {
Instantiate ("dotcyan",0,0);
} else if (0.2 <= i < 0.4) {
Instantiate ("dotred",0,0);
} else if (0.4 <= i < 0.6) {
Instantiate ("dotgreen",0,0);
} else if (0.6 <= i < 0.8) {
Instantiate ("dotyellow",0,0);
} else if (0.8 <= i < 1.0) {
Instantiate ("dotorange",0,0);
}
}
I have not seen seen that form of if(x < i < y) before, it may not be valid. Did you see that somewhere in the C# language reference?
Change that to
if( x <= i && i < y)
also where x or y is a decimal (i.e 0.4) make it 0.04f. Finally instantiate takes an Object, position and rotation and object is not a string, remove the quotes around dotCOLOR and take a look at Instantiate doc for position and rotation arguments