// Put this at the top, with the rest of your class variables
private var bullit : GameObject;
// now, in OnGUI, you set 'bullit' to the newly instantiated object
if (GUI.Button(Rect(450,280,80,80),"Rockets"))
{
if(shoottimer ==0){
bullit = Instantiate(rocketpref, GameObject.Find("rs").transform.position, Quaternion.identity);
shoottimer = shootcooler;
}
}
// Now, you can access 'bullit' because it will exist for more
// than a single GUI cycle, like it would in your original script.
// check for existance of a bullet, first :)
GUI.enabled = (bullit != null);
// Now, the button will only activate if a bullet exists.
if (GUI.Button(Rect(350,280,80,80),"cam"))
{
var childCam : Camera = bullit.GetComponentInChildren(Camera);
childCam.enabled = false;
}
GUI.enabled = true;
You need to be aware of variable scope. If a variable is declared inside a set of curly braces, it will exist only inside those braces! If you are modifying a variable inside of an if-statement, you need to make sure that the variable exists outside of that block if you intend to use it elsewhere.
if(condition)
{
var result = blah;
}
Debug.Log(result); // WILL NOT WORK!
var result = 0;
if(condition)
{
var result = blah;
}
Debug.Log(result); // Will print 'blah' only on the frame when 'condition' is true
var result = 0;
function Update()
{
if(condition)
{
result = blah;
}
Debug.Log(result); // Will print 'blah' on the frame when 'condition' is true,
// and then for every frame after that point, because the
// scope of 'result' extends *outside* of the function!
}