• 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 /
This question was closed Sep 30, 2012 at 09:39 AM by Fattie for the following reason:

Problem is not reproducible or outdated

avatar image
Question by Supershandy · Sep 27, 2012 at 04:03 PM · gameobjectarrayvariablejava

Access variables, objects in array in another script?

Sorry if the title seems confusing but I've either really overcomplicated t$$anonymous$$ngs or i'm just doing somet$$anonymous$$ng very stupid....anyway, here goes.

Basically what i'm trying to do is plot where a planet is in the solar system by ways of using it's semi major axis, mass etc. Now these values are in one script called "Planet" w$$anonymous$$ch contains the values

name, mass, semiMajorAxis, inclination, avgSpeed

with functions to return these named Getmass(), GetsemiMajorAxis() etc etc

In another script I have named "Kepler Coords", I have an array called "planet", now t$$anonymous$$s array contains prefab spheres with teh "Planet" script attached to them with the values filled in, In the "Kepler Coord" script I need to be able to access the variables of the "Planet" script, however try as I may I am getting no joy.

here is part of the "Kepler Coord" script,

 var planet : GameObject[];
 var planetScript : Planet;
 
 function Awake ()
 
 {
     var planet = GameObject.FindGameObjectsWithTag("planet");
     
 }
 
 private function BuildSystem ()
 
 {
     bodies = new GameObject[(planet.length -1)];
     
     for (var i : int = 0; i<planet.length; i++)
         {
             var tMass : float = mass * massConversion;
             mass += tMass;
             
             var tRadialDistance   : float = semiMajorAxis * distanceConversion;
             var tInclinationAngle : float = inclination * ((2.0*Mathf.PI) / 360);
             var tPositionX        : float = tRadialDistance * Mathf.Cos (tInclinationAngle);
             var tPositionY        : float = tRadialDistance * Mathf.Sin (tInclinationAngle);
             var tPosition         : Vector3 = new Vector3 (tPositionX, 0.0, tPositionY);
             
             var tVelocityZ : float = avgSpeed * (distanceConversion/timeConversion);
             var tVelocity  : Vector3 = new Vector3 (0.0, tVelocityZ, 0.0);
             
             var tBody : GameObject = Instantiate(planet, tPosition, Quaternion.identity);
             
             
             tBody.rigidbody.mass = tMass;
             tBody.rigidbody.velocity = tVelocity;

As you can see I need to access the variables from the "Planet" script, but whenever i try planet.GetComponent("mass") or planet.GetComponent(GetMass()) or planet.GetComponents("mass") etc I keep getting an error saying that "GetComponent is not part of GameObject[]".

Any help would be much appreciated as I'd rather not go down the XML route since I have no understanding of it.

Comment

People who like this

0 Show 1
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 Fattie · Sep 29, 2012 at 08:54 AM 0
Share

http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html

2 Replies

  • Sort: 
avatar image

Answer by flaviusxvii · Sep 27, 2012 at 04:42 PM

GetComponent() is to be used with the name of the script/class (the fact t$$anonymous$$s is the classname is $$anonymous$$dden from you in Unityscript), not the methods or members of the class.

So you'd do somet$$anonymous$$ng like `var kc : KeplerCoord = planet.GetComponent("KeplerCoord");` and then get the mass or whatever from the variable kc.

Comment

People who like this

0 Show 0 · 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

Answer by Supershandy · Sep 30, 2012 at 06:47 AM

I t$$anonymous$$nk i had just massively overcomplicated thngs as it said it could not access variables since they weren't static, so I change them all to static w$$anonymous$$ch just changed all the values to zero (Since I had put all the values in through the inspector).

I t$$anonymous$$nk for t$$anonymous$$s project a locally-acccessed XML is going to be the better option since there are going to be a lot of variables floating around and i'd rather try and keep them all in one place and keep the script levels down as much as possible.

Thnak you though for the help, it was much appreciated :)

Comment

People who like this

0 Show 0 · 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

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

10 People are following this question.

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

Related Questions

how can I display a variable as a GUIText 5 Answers

Sorting an Array of GameObjects by Name 1 Answer

How can I access a variable from another script in a separate object? 1 Answer

How to access gameObject variable script 2 Answers

Is this doable in unity 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