+5 votes

Hi,

So I'm having some trouble trying to figure out why I'm getting the error

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Godot.RigidBody'.

  <C Source>     :0
  <Stack Trace>  :0 @ ()
  Object.cs:338 @ System.Object Godot.Object.Call(System.String , System.Object[] )()
  cameraController.cs:52 @ void cameraController._Input(Godot.InputEvent )()

I've come to the conclusion that it's because it can't find my RigidBody node...
However, I am declaring that node like this:

public class test : Node {
    RigidBody player;
        public override void _Ready() {
            player = GetNode("/root/Scene/player") as RigidBody;

So when _Ready has been initialized, I should be able to fetch it from anywhere in my script, right? At least, I can do that with all my other scripts, but not with this one for some reason.

If I declare the RigidBody node right before I need to use it in my if statement, it works fine, e.g.:

public void blabla() {
    if ( true ) {
        RigidBody player = GetNode("/root/Scene/player");
        // do stuff with player node
    }

Anyone know what's up? I can't for the life of me figure out why it wont access that node. I'm not disposing of it anywhere, or using QueueFree().
Using 3.1 stable Mono.

in Engine by (120 points)

As a note, you can check if a reference is valid using IsInstanceValid(node).

This problem could happen when you change the scene tree. In this case, the Player reference you loaded in the _Ready is disposed because there is another Player instance (located in the same node path) loaded in a second scene. So, is this error happening when you change the scene?

Hello. I have the same error happening when I am changing the scene. Do you know how to solve this error? Thanks in advance !

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.