x


Static variables problem

i have a health bar script, and i made a static variable in it. i made it so a monster follows the main character. The monster's code also has a static variable. In the main character's code, i want to make it so when the monster hits it, it does the health bar's static variable minus the monster's static variable. the health bar's script is called bar, and the monster's script is called beholder. here's the code for the character:

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var turnSpeed : float = 3.0;
var gravity : float = 20.0;

bar.barDisplay = 100; 
beholder.damage = 5;

private var moveDirection = Vector3.zero;
private var turnDirection : float = 0;
private var grounded : boolean = false;

function OnTriggerEnter(hit : Collider)
{
    if(hit.gameObject.tag == "Beholder")
    {
        Debug.Log("hit for " + beholder.damage + "points");
        bar.barDisplay -= beholder.damage;
    }
}
function FixedUpdate() {
    if (grounded) {
        // We are grounded, so recalculate movedirection directly from axes
        moveDirection = new Vector3(0, 0, Input.GetAxis("Vertical"));
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= speed;

        turnDirection = Input.GetAxis("Horizontal");
        transform.Rotate(0, turnDirection * turnSpeed ,0);

        if (Input.GetButton ("Jump")) {
            moveDirection.y = jumpSpeed;
        }
    }

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    // Move the controller
    var controller : CharacterController = GetComponent(CharacterController);
    var flags = controller.Move(moveDirection * Time.deltaTime);
    grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)

everything else works, but unity says that the identifiers beholder and bar are unknown...

more ▼

asked May 22 '10 at 08:04 PM

oz m gravatar image

oz m
126 14 17 25

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

"beholder" and "bar" are not the names of your classes. (That is, they are not the names of the scripts.) The naming convention in Unity is to use uppercase for the first letter of a class's name. So here is what you would use in the script you posted:

Bar.barDisplay = 100; 
Beholder.damage = 5;

And in those scripts, named Bar, and Beholder, respectively, you need these declarations:

static var barDisplay;
static var damage;
more ▼

answered May 22 '10 at 08:20 PM

Jessy gravatar image

Jessy
15.7k 72 95 198

thanks, i didn't know that

May 22 '10 at 08:27 PM oz m

it didn't work.

May 22 '10 at 08:31 PM oz m

actually yes it did now, thanks

May 22 '10 at 08:47 PM oz m

useful to me too - thanks

Dec 04 '10 at 05:03 PM schwertfisch
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x356
x284
x72
x18

asked: May 22 '10 at 08:04 PM

Seen: 4300 times

Last Updated: May 22 '10 at 08:04 PM