Hi,
I am attaching a project to this post here and I am wondering if anyone could tell me why I am getting a ‘this._body is null’ error after I instantiate a prefab and try to set it’s parent.
What I have is a PlatformSpawner
component that initially instantiates a platform node, which continuously moves to the left the screen via its Rigidbody2D. There also an empty node in the middle of the screen that has its own collider so that when the platform touches it, a message is sent through my own event handler to the PlatformSpawner
to instantiate another platform. However, I get the ‘this._body is null error’, which I’ve tracked down to when I try to set the platform as a child of any node, really.
I don’t understand the error because I have both a RigidBody2D and a BoxCollider2D set up on the platform.Any help would be appreciated. The error and code is below, while the project link is above. Thanks!
The error message:
error { target: Window, isTrusted: true, message: “TypeError: this._body is null”, filename: “http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js”, lineno: 154773, colno: 11, error: TypeError, srcElement: Window, currentTarget: Window, eventPhase: 2, … }
bubbles: true
cancelBubble: false
cancelable: true
colno: 11
composed: false
currentTarget: null
defaultPrevented: false
error: TypeError: this._body is null
eventPhase: 0
explicitOriginalTarget: Window http://localhost:7456/
filename: “http://localhost:7456/scripting/engine/bin/.cache/dev/preview/bundled/index.js”
isTrusted: true
lineno: 154773
message: “TypeError: this._body is null”
originalTarget: Window http://localhost:7456/
returnValue: true
srcElement: Window http://localhost:7456/
target: Window http://localhost:7456/
timeStamp: 11130
type: “error”
<get isTrusted()>: function isTrusted()
: ErrorEventPrototype { message: Getter, filename: Getter, lineno: Getter, … }
index.js:1:2554
Relevant code posted below:
@ccclass('ACEventHandler')
export class ACEventHandler extends Component {
static instance:ACEventHandler;
constructor() {
super();
if (ACEventHandler.instance) {
return ACEventHandler.instance;
}
ACEventHandler.instance = this;
}
emitEvent(event:string, args?:any) {
this.node.emit(event, args);
}
registerEvent(event:string, callback:any, bind:any) {
this.node.on(event, callback, bind);
}
deregisterEvent(event:string, callback:any, bind:any) {
this.node.off(event, callback, bind);
}
}
@ccclass('Platform')
export class Platform extends Component {
@property({type:Vec2})
public force:Vec2 = new Vec2(-5, 0);
private _rb:RigidBody2D|null = null;
private _collider:BoxCollider2D|null = null;
start () {
if (!this._rb) {
this._rb = this.getComponent(RigidBody2D);
}
if (!this._collider) {
this._collider = this.getComponent(BoxCollider2D);
this._collider?.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
}
}
lateUpdate(deltaTime:number) {
if (this._rb) {
this._rb.linearVelocity = this.force;
}
}
onBeginContact(self:Collider2D, other:Collider2D, contact:IPhysics2DContact|null) {
if (other.group === (1 << 4)) {
ACEventHandler.instance?.emitEvent("spawn");
}
if (other.group == (1 << 5)) {
this.node.destroy();
}
}
}
@ccclass('PlatformSpawner')
export class PlatformSpawner extends Component {
@property({type:Prefab})
public platformPrefab:Prefab|null = null;
@property({type:Node})
public spawnPoint:Node|null = null;
private _speedMulitplier:number = 1;
// This is called through a game manager when needed. It works.
activate(active:boolean) {
if (active) {
ACEventHandler.instance?.registerEvent("spawn", this.spawnPlatform, this);
this.spawnPlatform();
}
else {
ACEventHandler.instance?.deregisterEvent("spawn", this.spawnPlatform, this);
}
}
spawnPlatform() {
if (this.platformPrefab) {
let platform:Node = instantiate(this.platformPrefab);
if (platform && this.spawnPoint) {
this.node.addChild(platform); // WHY IS THERE A THIS._BODY IS NULL ERROR?
platform.position = Vec3.ZERO;
}
}
}
}