Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 David 33 · May 16, 2011 at 10:33 PM · javascriptarrayobjects

(JS) Calling a member function of an object in an array

In javascript, I have an array "x", the first element of which is an instance of object "y". Elsewhere, I try to call a member function of y like so:

x[0].member();

It fails. How can I make this work?

Comment
Add comment · Show 2
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 Joshua · May 16, 2011 at 11:09 PM 0
Share

I don't really understand what you mean. Do you want to access components of object y? In that case use y.GetComponent("name"/type) for instance y.GetComponent(Transform);

avatar image DaveA · May 16, 2011 at 11:11 PM 0
Share

You should post some actual code. If x[0] is a gameobject, you might need to GetComponent('thescript').member()

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · May 16, 2011 at 11:10 PM

I guess you declared your array like this:

var x : Array;

The Array class can only hold elements of type Object. Everything is an Object so you can store everything in that array, but if you access an element the compiler only knows it's an Object. You have to cast the reference into your actual type.

(x[0] as MyScriptType).member();

The Array class is very slow due to dynamic typing. Native arrays are the fastest but they can't be resized that easily. The best alternatives are the generic classes like List.<>

var x = new List.<MyScriptType>();

The members of such a list have the type you specified at creation time, in my example "MyScriptType". With such a list your line above would work.

x[0].member();
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 David 33 · May 23, 2011 at 07:31 PM 0
Share

I am declaring my array as static var inventory = new Array();

I am calling it thusly: inventory[0] = i_crossbow; inventory[0].OnUse();

That last statement throws the following error: InvalidCastException: Cannot cast from source type to destination type.

I apologize for the lateness of my reply.

avatar image
1

Answer by jahroy · May 16, 2011 at 11:10 PM

That should definitely work. I do it all the time.

I wonder if your array needs to be typed (declared to be a specific type) or if it has to be a built-in array.

The way you word your question it sounds like your array might contain objects of different types. I could see how this could cause issues for UnityScript, which can be loose with it's types.

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

No one has followed this question yet.

Related Questions

Getting a variable from a GameObject inside a 2d array? 1 Answer

How do you debug arrays of objects? 1 Answer

Need help using GUI.Button/ input storage 1 Answer

Convert Array into One String (Js) 1 Answer

Help me Convert JS to C# 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