[SOLVED] Camera object set to null?

I am trying to pick an object using Collisioning using the code below:

Gamemanager.attributes.add("Camera", {type: "entity"});

// initialize code called once per entity
Gamemanager.prototype.initialize = function() {

    //Look for mouse down event
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
};

Gamemanager.prototype.onSelect = function(e){
    if(canSelect){
        var from = this.Camera.entity.camera.screenToWorld(e.x, e.y, this.Camera.entity.camera.nearClip);
        var to = this.Camera.entity.camera.screenToWorld(e.x, e.y, this.Camera.entity.camera.farClip);

        var result = this.app.systems.rigidbody.raycastFirst(from, to);
        if (result) {
        var pickedEntity = result.entity;
        console.log(pickedEntity.name);
        //pickedEntity.script.pulse.pulse();
    }
  }
};

I have already parsed the script and added the camera in the editor. For completeness, I have also added a collision component to the object being clicked. When i click on the object, I get the error code Cannot read property ‘entity’ of null. Any ideas?

Hi @aperez440,

this.Camera is already an entity, no need to use .entity again:

        var from = this.Camera.camera.screenToWorld(e.x, e.y, this.Camera.camera.nearClip);
        var to = this.Camera.camera.screenToWorld(e.x, e.y, this.Camera.camera.farClip);

Hey, I appreciate the reply. I’m still getting the null reference after making that change however.

Have you dragged and dropped a camera entity in editor on that attribute?

Yeah, I parsed and its in there already. Not sure what’s going on

Maybe I should create a var to reference the camera first.Like this:

Var thisCamera;

Gamemanager.prototype.initialize = function(){
     thisCamera = this.entity.camera;
};

then do the rest

Can you share a sample project that reproduces the issue?

Here is the editor link: https://playcanvas.com/editor/scene/1134488

Code is in the Gamemanager script. I think I did that right?

I see you use this.thisCamera in your script instead of just thisCamera. If you want to use this.thisCamera you have to add this.thisCamera = this.entity.camera; to your initialize function.

1 Like

yeah Im just trying to see whats wrong. I just removed the this and its still throwing the null reference.

Gamemanager.prototype.onSelect = function(e){
    if(canSelect){
        var from = thisCamera.camera.screenToWorld(e.x, e.y, thisCamera.camera.nearClip);
        var to = thisCamera.camera.screenToWorld(e.x, e.y, thisCamera.camera.farClip);

        var result = this.app.systems.rigidbody.raycastFirst(from, to);
        if (result) {
        var pickedEntity = result.entity;
        console.log(pickedEntity.name);
    }
  }
};

Additionally, I added the `this.thisCamera = this.entity.camera; to initialize and created a var thisCamera and then changed the code to

Gamemanager.prototype.onSelect = function(e){
    if(canSelect){
        var from = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.nearClip);
        var to = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.farClip);

        var result = this.app.systems.rigidbody.raycastFirst(from, to);
        if (result) {
        var pickedEntity = result.entity;
        console.log(pickedEntity.name);
        //pickedEntity.script.pulse.pulse();
    }
  }
};

But still getting null. How odd.

Probably because thisCamera is already your camera component. Right now you try to get the camera component of the camera component and that result is null.

Alright alright, I think I’m getting closer. After removing the superfluous camera and now having this

Gamemanager.prototype.onSelect = function(e){
    if(canSelect){
        var from = this.thisCamera.screenToWorld(e.x, e.y, this.thisCamera.nearClip);
        var to = this.thisCamera.screenToWorld(e.x, e.y, this.thisCamera.farClip);

        var result = this.app.systems.rigidbody.raycastFirst(from, to);
        if (result) {
        var pickedEntity = result.entity;
        console.log(pickedEntity.name);
        //pickedEntity.script.pulse.pulse();
    }
  }
};

I get the error Cannot read property ‘screenToWorld’ of null

Did you initialize this.thisCamera or still only thisCamera? Also do you know the difference between the two ways of initialize?

I have a var thisCamera; in global and this.thisCamera = this.entity.camera; in initialize

You use the script on your GameManager entity but that entity has no camera component on it. So there is no this.entity.camera.

I think you can do this.thisCamera = thisCamera. Or just use thisCamera in your script, because I don’t think you need this.thisCamera.

Ok. I have scriptable attribute

//Create an attribute to make a reference to the camera
Gamemanager.attributes.add("thisCamera", {type: "entity"});

Nothing in initialize.

and the script is

Gamemanager.prototype.onSelect = function(e){
    if(canSelect){
        var from = thisCamera.screenToWorld(e.x, e.y, thisCamera.nearClip);
        var to = thisCamera.screenToWorld(e.x, e.y, thisCamera.farClip);

        var result = this.app.systems.rigidbody.raycastFirst(from, to);
        if (result) {
        var pickedEntity = result.entity;
        console.log(pickedEntity.name);
        //pickedEntity.script.pulse.pulse();
    }
  }
};

I get the error code: thisCamera is not defined. But I shouldnt need to declare it in initialize if I have it in a sciptable attribute right?

In this case it should be this.thisCamera.camera.

Alright. It looks like this

Gamemanager.prototype.onSelect = function(e){
    if(canSelect){
        var from = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.nearClip);
        var to = this.thisCamera.camera.screenToWorld(e.x, e.y, this.thisCamera.camera.farClip);

        var result = this.app.systems.rigidbody.raycastFirst(from, to);
        if (result) {
        var pickedEntity = result.entity;
        console.log(pickedEntity.name);
        //pickedEntity.script.pulse.pulse();
    }
  }
};

I parsed and made sure the camera is in there. I still get: Uncaught TypeError: Cannot read property ‘camera’ of null.

Your set up seems to be correct right now. I see the first time it’s also working without any error, only after the first time there is a problem. What do you do when you move the cubes?