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 pdunton · Aug 02, 2014 at 12:16 AM · javascriptarray

Grab a variable from each planet in scene and put it into an array

Hi, I have a scene full of planets, and for something else I need to grab the variable "force" from a script called "Gravity2" attached to each of them and put those variables into an array called "Forces". I have tried a lot of things and I've just ended up confusing myself. The planets are the only non-GUI thing in the scene, and by planets I am talking about all objects with the tags "RedPlanet" "BluePlanet" "RedSun" "BlueSun" and "BlackHole". Just some ideas would be very helpful, thanks! Here is the script.

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 static function CreatePath(a : float , v : float, x : float, obj : GameObject) : Vector3[]{
     var points : Vector3[] = new Vector3[4];
     
     //Creates Line
     for(var i : int = 0; i < 3; i++){
         var val : int = i + 1;
         points[i] = Vector3(val * v * Mathf.Cos(a), val * v * Mathf.Sin(a), 0);
         if(x > 0){
             points[i].x *= -1;
         }
     }
     for(var current in points){
         current += obj.transform.position;
     }
     //End Create Line
     
     //Add Net Force
         Forces(obj);
         
     //End Add Net Force
     
     return points;
 }    
 
 static function Forces(obj : GameObject){
 
     var planets : Array = GameObject.FindGameObjectsWithTag("BluePlanet");
     planets.Add(GameObject.FindGameObjectsWithTag("RedPlanet"));
     planets.Add(GameObject.FindGameObjectsWithTag("BlueSun"));
     planets.Add(GameObject.FindGameObjectsWithTag("RedSun"));
     planets.Add(GameObject.FindGameObjectsWithTag("BlackHole"));
     
     var forces : Array;
     
     if(planets.length > 0){
     
     
         for(var planet in planets){
             var planetGravity : Gravity2 = planet.gameObject.GetComponent("Gravity2");
             var force : Vector2 = planetGravity.force;
             
             forces.Add(force);
         }
     }
 }

 








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 ShadyProductions · Aug 02, 2014 at 03:17 PM 0
Share

What about making the force var in the gravity script public so it shows up in the inspector, that way you can probably take it from the planet object itself I believe

avatar image pdunton · Aug 03, 2014 at 01:51 AM 0
Share

The main problem I have right now is referencing the planet inside my for loop at lines 48-53 because the error says "gameObject is not a member of Object". This happens because the planets array is a Javascript array ins$$anonymous$$d of the built in arrays so when I reference planet it sees planet as an Object and not a GameObject.

0 Replies

· Add your reply
  • Sort: 

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Obtaining a Array with Unique Items 1 Answer

Edit Prefab, Instantiate, Access Array in Clone 1 Answer

Turn all objects in the array to make same thing 0 Answers

Store an array inside of an array 1 Answer

Is it possible to sort arrays based upon a previous sort? 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