- Home /
Help with flappy bird random spawn
Whenever I debug the code it says "unknown identifier y"
var x = 3;
var obsicale : GameObject;
var randomize : int = Mathf.Abs(Random.Range(-3,3));
function Start () {
InvokeRepeating("spawn",1f,1.5f);
}
function Update () {
var y = Random.Range(-3f, 3f);
}
function spawn()
{
Instantiate(obsicale,new Vector3(x * 3f, y * 1),Quaternion.identity);
}
Answer by robertbu · Jul 23, 2014 at 05:42 PM
You are declaring 'y' inside of Update(). Any variable declared inside a function is created when the function is called and destroyed when the function is exited. If you want more about local function variables, Google "execution stack" and or "call frame". To fix your problem, you need to declare 'y' at the top of true file like you do 'x'. Note you want to be careful how you initialize things when using the 'var' keyword. The type of the object is taken from what you assign (or not assign). So for example:
var x = 3;
...makes 'x' and integer. Also Random.Range(-3, 3) uses the integer version of the function, not the floating point version since both parameters passed are integers.
Answer by HamzaSiddiqui23 · Jul 23, 2014 at 06:15 PM
Initialize the variable Y where you have initialized the variable x. That should take care of this.