• 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 trriley · Sep 29, 2011 at 02:42 PM · getcomponent

Get array values from other script

I have an array of game objects i set in a c# script . later i need the array of objects in a different java script that is not attached to the same object as the c# script. how can i get the array from one c script to the java script?

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

1 Reply

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

Answer by syclamoth · Sep 29, 2011 at 03:00 PM

There are a few ways of doing this!

The first is to assign a known, unique tag to the object with the C# script on it (say "arrayObject") and then use

 var gameObjectArray : GameObject[] = GameObject.FindWithTag("arrayObject").
              GetComponent("nameOfC#Script").whateverYouCalledTheArray;

from any javascript component in the scene.

Another way to do it (a bit more specific) is to have a direct reference to the gameObject in question (instead of using FindWithTag)

so in your javascript-

 var arrayObject : GameObject

 /* whatever code*/

 var gameObjectArray : GameObject[] = arrayObject.GetComponent("nameOfC#Script").
                                           whateverYouCalldTheArray;

There are a few other ways, but they become more and more complex, and require more and more coding knowledge to pull off.

A thing to remember, is that javascript objects cannot see C# scripts at compile time. They have to communicate through awkward lookups and messenger classes, which is not very efficient. If you absolutely must be using C# for one of your scripts, you should use it for all of them- it is much cleaner and easier that way. For example, it would be possible to refer to the script directly, instead of having to rely on gameObject.GetComponent().

Yet another thing- the arrays returned here are builtin arrays- not Unity's ArrayList wrapper which is sometimes called the same thing. They are fixed-size, and don't have any of the special functionality of the Array class.

Comment
Add comment · Show 4 · 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 Eric5h5 · Sep 29, 2011 at 10:11 PM 0
Share

It's incorrect that JS scripts can't see C# scripts. Please refer to the docs on script compilation order. Anyway, you always need to use GetComponent to refer to other scripts regardless. (Unless you're using static variables, but that's a special case which generally isn't typical.)

avatar image syclamoth · Sep 30, 2011 at 12:57 AM 0
Share

There's a difference between using GetComponent(string) and GetComponent(type). While they both work fine, one of them is somewhat faster.

avatar image Eric5h5 · Sep 30, 2011 at 01:39 AM 0
Share

Well, yes, but that's not relevant here. Aside from speed, using type ins$$anonymous$$d of string allows for compile-time error checking, as well as returning the type ins$$anonymous$$d of Component (when using JS). But as I said, that's beside the point.

avatar image Bonifatius GK · Dec 05, 2012 at 08:12 AM 0
Share

well then, if i have a script that attached to the parent, and i need to access a struct which is have already filled with data in parent's Start() function... can i do that..?? if yes, then how it will be..?? I've try the answer, but i still can't access my struct(public) variable..

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Is it possible to assign the script for GetComponent in the editor? 1 Answer

How do I GetComponent from a NetworkView? 0 Answers

GetComponent not working C# 1 Answer

A simple CS0309 problem... 2 Answers

Using raycast - Why can't I get a variable on the object I hit? 1 Answer


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