Adjusting ObjectLabel to activate animations

I am using the ObjectLabel script from wiki unify to keep a GUI texture floating over a characters in my scene. But I cannot work out how to use that GUI textures to behave as a trigger so as to trigger animation in the character it is floating over. I have adapted the ObjectLAbel script like this

var piggy_a : newArray("jump";"hop","dance");

var target : Transform;  // Object that this label should follow

var offset = Vector3.up;    // Units in world space to offset; 1 unit above object by default
var clampToScreen = false;  // If true, label will be visible even if object is off screen
var clampBorderSize = .05;  // How much viewport space to leave at the borders when a label is being clamped
var useMainCamera = true;   // Use the camera tagged MainCamera
var cameraToUse : Camera;   // Only use this if useMainCamera is false
private var cam : Camera;
private var thisTransform : Transform;
private var camTransform : Transform;

function Start () {
    thisTransform = transform;
    if (useMainCamera)
        cam = Camera.main;
    else
        cam = cameraToUse;
    camTransform = cam.transform;
}

function Update () {
    if (clampToScreen) {
        var relativePosition = camTransform.InverseTransformPoint(target.position);
        relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
        thisTransform.position = cam.WorldToViewportPoint(camTransform.TransformPoint(relativePosition + offset));
        thisTransform.position = Vector3(Mathf.Clamp(thisTransform.position.x, clampBorderSize, 1.0-clampBorderSize),
                                         Mathf.Clamp(thisTransform.position.y, clampBorderSize, 1.0-clampBorderSize),
                                         thisTransform.position.z);
    }
    else {
        thisTransform.position = cam.WorldToViewportPoint(target.position + offset);

        function OnGUI() {
   if(GUI.Texture){
       animation.CrossFade(piggy_a[Random.Range(0,piggy_a.length)]);
   }
}

    }

just putting a new array at the top and at the bottom trying to blend the script i would use for a conventional button. But i do not know how to make the floating texture the active GUI button. I dont know enough about scripting.

  1. You cannot define your OnGUI function inside of another function.
  2. You're missing a brace.
  3. GUI doesn't have a property called Texture.
  4. You cannot have a semi-colon inside the array declaration.
  5. Are you animating this transform? Does this transform have those animations on it even?

You did more than just add an array. The changes you made to the original code are a mess and won't work. If you make changes, test them as you go and fix any errors.

Using a GUITexture

If you really want to use a GUITexture use one. If you want to add interaction, then you would implement OnMouse functions like OnMouseUp(). You only needed 3 changes to the original to make it do what you wanted:

//Add this
var anims : String[] = ["jump","hop","dance"]; //animations

//Add this
function OnMouseUp() {
    target.animation.CrossFade(anims[Random.Range(0,anims.length)]);
}

//Change this at the end
@script RequireComponent(GUITexture)

Using a GUI.Button

The way the original script worked was that it would have an attached GUIText object. To use a button, you don't need to worry about the transform.

This will do generally what you want if you attach it to something you want the button to follow:

var anims : String[] = ["jump","hop","dance"]; //animations
var buttonTexture : Texture; //The texture to use on the button
var offset = Vector3.up; //World space offset. 1 unit above object by default
var clampToScreen = false; //Will the label be visible if object is off screen
var clampBorderSize = .05; //screen space to leave when clamped
var useMainCamera = true; //Use the camera tagged MainCamera
var cameraToUse : Camera; //Camera to use if useMainCamera is false
private var cam : Camera; //The camera we're using
private var screenPos : Vector3; //The screen position of the button

function Start () {
    if(useMainCamera) cam = Camera.main;
    else cam = cameraToUse;
}

function Update () {
    if(clampToScreen) {
        var relativePosition = cam.transform.InverseTransformPoint(transform.position);
        relativePosition.z = Mathf.Max(relativePosition.z, 1.0);
        screenPos= cam.WorldToScreenPoint(cam.transform.TransformPoint(relativePosition + offset));
        screenPos= Vector3(Mathf.Clamp(screenPos.x, clampBorderSize,Screen.width-(clampBorderSize+buttonTexture.width)),Mathf.Clamp(screenPos.y,clampBorderSize,Screen.height-(clampBorderSize+buttonTexture.height)),screenPos.z);
    }
    else screenPos= cam.WorldToScreenPoint(transform.position + offset);
}

function OnGUI() {
    if(GUI.Button(Rect(screenPos.x,screenPos.y,buttonTexture.width,buttonTexture.height),buttonTexture))
        animation.CrossFade(anims[Random.Range(0,anims.length)]);
}