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 /
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
0
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 things or i'm just doing something 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" which 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 this 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
Add comment · 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
0

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

GetComponent() is to be used with the name of the script/class (the fact this is the classname is hidden from you in Unityscript), not the methods or members of the class.

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

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 Supershandy · Sep 30, 2012 at 06:47 AM

I think 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 which just changed all the values to zero (Since I had put all the values in through the inspector).

I think for this 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
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

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 to deactivate something when holding down a key? 1 Answer

Access a String array in one script from another script 0 Answers

How do I create an array for multiple targets? 1 Answer

C# Make array based on script variable 1 Answer

Array of GameObjects created with a Script 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