Button

Simple question, how would i make a button so when the user clicks on run a function.

var labeltext : String = "Welcome";

function OnGUI () {
    if (GUI.Button(Rect(10,10,100,40), "Hello")) {
        DoSomething();
    }	
	
    GUI.Label(Rect(10,60, 100, 40), labeltext);
}


function DoSomething() {
    labeltext = iPhoneInput.acceleration.ToString();
}

hope this helps! If you tap the button it changes the “Welcome” text to the last measured linear acceleration of the iphone in three-dimensional space. :slight_smile:

now if someone could tell me ho to access a custom function like this from another script on another object without running into an Error like “BCE0019: ‘DoSomething’ is not a member of ‘Component’.” this would be a happy morning…

col000r,

you have a typing problem somewhere, iphone unity doesn’t support dynamic typing.

here’s an example of what threw the error for me.

var cubeToColor = gameObject.Find("cube"+i).GetComponentInChildren(Transform);
		for (var colorCube  in cubeToColor) {
							colorCube.renderer.material.color = Color(redColor_h,greenColor_h,blueColor_h,1.0);

I needed to type the variable colorCube as a Transform

so that line needed to read

for (var colorCube : Transform in cubeToColor) {

how would I fix this so that It works, right now it crashes my app on start up.

var Color : Color;
var fpsBool = true;

function OnGUI () {
Color = RGBSlider (Rect (100,100,200,10), Color);

fpsBool = GUI.Toggle (Rect (250, 100, 100, 30), fpsBool, "Show FPS?");
}

function Start () {
	fpsBool = false;
}
function Update () {
	(GameObject.Find("Green")).renderer.material.color = Color;
	
	if (fpsBool == true) {
		(GameObject.Find("FPS")).guiText.enabled = true;
		}
		else {
		(GameObject.Find("FPS")).guiText.enabled = false;
		}			
}

function RGBSlider (screenRect : Rect, rgb : Color) : Color {
	rgb.r = GUI.HorizontalSlider (screenRect, rgb.r, 0.0, 1.0);
	screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping
	rgb.g = GUI.HorizontalSlider (screenRect, rgb.g, 0.0, 1.0);
	screenRect.y += 20; // <- Move the next control down a bit to avoid overlapping
	rgb.b = GUI.HorizontalSlider (screenRect, rgb.b, 0.0, 1.0);
	return rgb;
}