x


Difficulties accessing other components

You know, gents, I'm not quite sure that the manual/scripting reference entries on this stuff is very helpful... Anyway.

I'm currently trying to streamline the process of damaging enemies in order to remove some issues that pop up. What I'm wanting to do is create a master array script that has a number of arrays for Enemies, Items, and the like. That works just fine; the issue is accessing said arrays in other scripts. Now, the coding for damage is like this:

The part attached to the player that has the damage amount, as well as the part that actually sends the damage (Attack.js):

function normalSwing () {
    //unimportant stuff
    SendDamage(attackStrength);
    //unimportant stuff

}

function SendDamage (Damage : int) {
    for(var Enemy in enemies)
      Enemy.SendMessage("damageEnemy", Damage, SendMessageOptions.DontRequireReceiver);

}

And the part on the enemy that receives the damage (enemyHealth.js):

function damageEnemy (Damage : int) {
    if(isInRange) {
       enemyHealth -= Damage;
       print("Hit for" + Damage + " Damage!");
    }
}

(isInRange is determined by whether or not the enemy is within a certain hitbox)

And then the master array (LevelArrays.js):

var enemies : GameObject[];
var items : GameObject[];


function Awake () {
    enemies = GameObject.FindGameObjectsWithTag("Enemy");
    items = GameObject.FindGameObjectsWithTag("Item");

}

I need to be able to access the Enemy array from the LevelArrays script in the Attack script, but even with the information provided on using GetComponent and the like, I'm completely in the dark on how to do this. What say you?

more ▼

asked Jun 26 '11 at 06:38 AM

McMutton gravatar image

McMutton
121 27 31 38

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

4 answers: sort voted first

Making the arrays static in JS will effectively make them function as a singleton. What I usually do is set up one object as a sort of master object with a particular tag that I would call GameObject.FindWithTag during Start or Awake to grab a reference to and store within a private var within the client script.

In this way, you just access them like this

private static var levelArrays:LevelArrays=null;

function Awake(){
if(!levelArrays){
var gc:GameObject.FindWithTag("GameController");
levelArrays=gc.GetComponent(LevelArrays);
}}

Then, just call the desired arrays and methods in the master arrays script as described in Dreamblur's response.

more ▼

answered Jun 26 '11 at 10:00 AM

TomHunt gravatar image

TomHunt
138 4 4 10

Ah, it works! Brilliant! Thanks, guys.

Jun 26 '11 at 04:37 PM McMutton
(comments are locked)
10|3000 characters needed characters left

I always try to encapsulate the data where it belongs - in objective oriented style. That is, I keep the logic within the object.

I do maintain an array in my "GameLoopController"(GLC) script too. But if its inside the GLC, then the other objects doesnt have access to it.

Why?

See it like a tree, the root knows its children and asks them. Then I make "public" functions in the objects that allows me to ask them if they are hit, if they give points etc.

If I need them to return upon an event I utilize eventlistners.

So regarding your question.

I believe you need to make the arrays PUBLIC, in C# you just add the word: public in front of the variables when declaring them.

But I still dont see why your main loop should make this array public accessable to your other objects, unless its a bit messy object hierachy your are building?

more ▼

answered Jun 26 '11 at 12:29 PM

BerggreenDK gravatar image

BerggreenDK
2.4k 54 62 75

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

Make your arrays static.

static var enemies : GameObject[];
static var items : GameObject[];

To access from any script:

LevelArrays.enemies[0]

(I've been using C# more than UnityScript lately, so if my brain somehow mixed things up, then just post a comment and I'll take out my UnityScript hat for a bit. =P)

more ▼

answered Jun 26 '11 at 07:01 AM

Dreamblur gravatar image

Dreamblur
703 9 16 22

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

Use a singleton pattern to access your enemies and items. I donno how to implement it in javascript (I script in C#) but I saw this link. Hope this helps

more ▼

answered Jun 26 '11 at 08:35 AM

BigBulle gravatar image

BigBulle
202 9 12 21

(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:

x1396
x422
x314
x49

asked: Jun 26 '11 at 06:38 AM

Seen: 710 times

Last Updated: Jun 26 '11 at 04:37 PM