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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by MaxiKing480 · Mar 21, 2014 at 07:17 PM · gravityscript errorerror-message

Objects acting under each other's gravity. Test

I want to test the revised script from http://answers.unity3d.com/questions/196749/objects-acting-under-each-others-gravity.html (script from the best answer). I've completed the script, but it doesn't work:

 #pragma strict
 
 var planet : Transform;
 var numberOfPlanets = 10;
 var minimumSize = 1;
 var maximumSize = 10;
 var position = Vector3.zero;
 var planets = new Array ();
 var masses = new Array ();
 var total = Vector3.zero;
 var speed = Vector3.zero;
 static var G = 0.01f; // adjust with try & error
 static var planets = List.<Rigidbody>();
 private var myRigidbody : Rigidbody;
  
 //for testing. Set the z value to give the planet an initial speed along it's z-axis
 public var initialForwardSpeed : Vector3;
 function Awake()
 {
     myRigidbody = rigidbody;
     myRigidbody.velocity = transform.TransformDirection(initialForwardSpeed);
 }
  
 function OnEnable()
 {
     planets.Add(rigidbody);
 }
  
 function OnDisable()
 {
     planets.Remove(rigidbody);
 }
  
 function FixedUpdate()
 {
     var pos = myRigidbody.position;
     var acc = Vector3.zero;
     for(var planet in planets)
     {
         if (planet == myRigidbody)
             continue;
         var direction = (planet.position - pos);
         acc += G * (direction.normalized * planet.mass) / direction.sqrMagnitude; 
     }
     myRigidbody.velocity+= acc * Time.fixedDeltaTime;   
 }
 function calculate ()
 {
     for (k=0;k<numberOfPlanets;k++)
     {
         var indiv = planets [k];
         total = Vector3.zero;
         for(j=0;j<numberOfPlanets;j++)
         {
             var planetRef = planets[j];
             var direction = (planetRef.position-indiv.position);
             total += (direction.normalized * masses[j]) / direction.sqrMagnitude;
             speed[k] += total;
             indiv.transform.Translate (speed[k]);
         }
     }
 }

Error message: Assets/Script.js(13,12): BCE0089: Type 'Script' already has a definition for 'planets'. If I delete a variable, I get much more errors. What should I do?

Comment
Add comment · Show 3
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 Benproductions1 · Mar 22, 2014 at 07:43 AM 0
Share

You can't have two variables with the same name, that's just crazy! How should the compiler know which variable you want to use?

avatar image MaxiKing480 · Mar 23, 2014 at 10:46 AM 0
Share

Sorry, I'm a total beginner when it comes to Scripting. Which variable(s) should I rename or delete? Thanks in advance.

avatar image Benproductions1 · Mar 24, 2014 at 07:51 AM 0
Share

I just said you can't have two variables with the same name, therefore any variables which are defined more than once.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by screenname_taken · Mar 23, 2014 at 02:05 PM

One of the two "planets" one. Just change it to something else, in all the lines referring to that one. If you change the planets variable in line 13 for example, you should change the planets variable in lines 26,31,40 that refer to rigidbodies.

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

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

Error building Player because scripts had compiler errors 0 Answers

Js: UnityEngine.Rigidbody is required, how to fix? 2 Answers

Error on compiling unity headlookController. 0 Answers

Monodevelop Error- Unity 4.3 0 Answers

Gravity on object not working correctly 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