Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by McMutton · Jun 26, 2011 at 06:38 AM · arraygetcomponentcomponentaccessing

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by TomHunt · Jun 26, 2011 at 10:00 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image McMutton · Jun 26, 2011 at 04:37 PM 0
Share

Ah, it works! Brilliant! Thanks, guys.

avatar image
1

Answer by Dreamblur · Jun 26, 2011 at 07:01 AM

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by BerggreenDK · Jun 26, 2011 at 12:29 PM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by BigBulle · Jun 26, 2011 at 08:35 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get variable in script got by get component c# 2 Answers

Create array of scripts 1 Answer

C# accessing an array from a script in a different gameobject outside of the awake function 0 Answers

GetComponent with arrays C#? 0 Answers

GetComponent of ALL clones? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges