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 shrapnel92 · May 17, 2014 at 10:37 AM · gravityfalling

Why is my GameObject just falling in space after creation?

Hi all, I've only been learning C# for about a week so bear with me here...

I create a sphere and I just want it to hang in space, this is my code:

         void Start ()
         {
                 for (int y = 0; y < 1; y++) {
                         for (int x = 0; x < 1; x++) {
                                 GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
                                 sphere.AddComponent<Rigidbody> ();
                                 rigidbody.useGravity = false;
                                 sphere.transform.position = new Vector3 (0, 0, 0);
                         }
                 }
         }

I figured that the useGravity bit would stop it from falling, but after creating it, it's still got Gravity enabled until I pause it and turn it off, anyone know how to just make it hang there without falling?

Thanks!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Maskerov · May 17, 2014 at 10:49 AM

I have tested your code and it works. Look if you have added the script to the sphere and also the rigidbody. You can also set the is Kinematic true. Hope this helps

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 meat5000 · May 17, 2014 at 11:27 AM

I'm not 100% on this but I'm sure that you can't disable gravity on the next line as the RigidBody won't be created until the next frame. I could be wrong.

Try something like this directly after addcomponent to test the theory:

 if(rigidbody)
 {
      rigidbody.useGravity = false;
      Debug.Log("Rigidbody exists");
 }
 else
      Debug.Log("NO rigidbody");
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 AlucardJay · May 17, 2014 at 11:45 AM

I'm guessing its because the added component isn't initialized yet (it takes longer for AddComponent to finish setting up than for the script to continue and set useGravity). Try getting a reference to the added component and change it there :

 GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
 sphere.AddComponent<Rigidbody>();
 
 Rigidbody rBody = sphere.GetComponent<Rigidbody>();
 rBody.useGravity = false;
 
 sphere.transform.position = new Vector3 (0, 0, 0);
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 samf1111 · Jul 10, 2021 at 12:51 PM

You need to add collider.

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

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

Related Questions

character gravity problem 1 Answer

Endless "Falling in place" Simulation Question 0 Answers

My object is falling through the platform 1 Answer

Freeze constrains bug or idk 0 Answers

CharacterController how to disable acceleration of falling when moving in the air? 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