Jump to content

Accessing Rigidbodies [SOLVED]


Recommended Posts

I'm trying to canabalize flywlyx's GN Drive Mod to re-create an Anti-Grav mod I made a few years ago, but I think there's been a Unity update since then that's messed with what works.

The issue I'm having it with it is related to this code:

foreach (Part p in this.vessel.parts)
            {
                if ((p.physicalSignificance == Part.PhysicalSignificance.FULL) && (p.rigidbody != null))
                {
                    p.rigidbody.AddForce(-gee * p.rigidbody.mass);
                }
            }

It's telling me that there is no lowercase "rigidbody" in Parts, but trying uppercase Rigidbody, RigidBodyPart, or rb all don't have the desired effect.

I even tried downloading the original GN drive mod to see if that's broken, but that works fine.

Can anyone sleuth out what I'm doing wrong? I'm running Visual Studio 2017, Framework set to 3.5, and the class I'm building has inherited from PartModule.

Link to comment
Share on other sites

On 7/30/2019 at 7:35 PM, Tompete Kerman said:

It's telling me that there is no lowercase "rigidbody" in Parts, but trying uppercase Rigidbody, RigidBodyPart, or rb all don't have the desired effect.

The GameObject.rigidbody get method was removed in a (semi) recent Unity update.  You instead need to pull a reference to the component with GetComponent().

E.G.

Rigidbody rb = part.GetComponent<RigidBody>();

Cache the results if you need access more than once, as it is fairly expensive to use getComponent every frame.

Edit:  check the casing on my example; I don't remember if its 'Rigidbody' or RigidBody'... but it is only one of those...

 

Edited by Shadowmage
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...