function reset(){
var appC = this.app; var entityCamera= new pc.Entity();
this.entityCamera= appC.root.findByName(‘CameraMain’);
var posCam = new pc.Vec3(2.52, 0.266 , 1.598); var rotCam = new pc.Quat(4.81, 83.53,0);
this.entityCamera.setLocalPosition(this.posCam.x, this.posCam.y, this.posCam.z);
this.entityCamera.setLocalEulerAngles(this.rotCam);
}
gives me:
“Cannot read property ‘root’ of undefined”
We are actually coming back to my old problem -> making useful global variables. Right now I am shoving all my scripts in upon one hierarchy-entity only.
The ‘CameraMain’ is - for instance - the placeholder for the script above … does it hereby imply that the lines:
“var appC = this.app; var entityCamera= new pc.Entity();
this.entityCamera= appC.root.findByName(‘CameraMain’);” will fail due to the recurrance? (referrance back to own script-bearing entity)
It checks the if orbitCamera script object exists. It’s no different to if (this.entity.script.orbitCamera != undefined) given the code you have posted.
Ok I remember/understand my thumbs-up for the other post (Other-script referal (Global variables)) as being a bit lucky (that why I deemed it solved back then)
Now:
var HotelShifter = pc.createScript(‘hotelShifter’);
// initialize code called once per entity
HotelShifter.prototype.initialize = function() {
var entityDetailHotel = new pc.Entity();
this.entityDetailHotel = this.app.root.findByName(‘Hotel23’);
var entityMinimHotel = new pc.Entity();
this.entityMinimHotel = this.app.root.findByName('Hotel_Minim');
this.ambientLight= this.entity.script.ambientLight;
if (this.ambientLight) {
var self = this;
}
};
// update code called every frame
HotelShifter.prototype.update = function(dt) {
if(this.ambientLight.goCamera3w ===true){
console.log(‘OtherScript’);
this.entityDetailHotel.enabled = false;}
};
gives me >>
“TypeError: Cannot read property ‘goCamera3w’ of undefined”
where ambientLight is the script laying on another entity
The filenames in the left pane? That’s just the physical file name. The script ‘name’ you should be using when trying to reference them is what is passed to the function of createScript.
eg
var HotelShifter = pc.createScript(‘hotelShifter’);
The image shows an entity that doesn’t have an ambientLight script or hotelShifter script attached so I’m not quite sure what you are referring to? The entity in the image has mouseInput and orbitCamera.
When making replies like this, can you quote the text/post that you are answering to please? It helps when you have multiple questions going on in the thread.
If you can post a link to the project, it makes it much easier for people work out what’s the issue.
The issue is that I am trying (with all my energy) to make scripts across different entities, read each others variables. On the previous single image above, the two scripts were not on different entities.
On this double printscreen, one can see that I have put the scripts on different entities. And then the project fails (no orbit function when launching).
Ok, thanks - I can tell that you moved the mouseInput script to Root
Unfortunately I already tried that without luck …
Here is my project: https://playcanvas.com/editor/scene/680498
Press ‘Western coves’ and let it animate, after 6-8 sec the console.log (at F12-debug) should say true instead of false in accordance to.
var HotelShifter = pc.createScript(‘hotelShifter’);
// initialize code called once per entity
HotelShifter.prototype.initialize = function() {
var app = this.app;
var entityDetailHotel = new pc.Entity();
this.entityDetailHotel = this.app.root.findByName('Hotel_23');
var entityMinimHotel = new pc.Entity();
this.entityMinimHotel = this.app.root.findByName('Hotel_Minim');
var entityCamera = new pc.Entity();
this.entityCamera = this.app.root.findByName('CameraMain');
this.amibientLight= this.entityCamera.script.amibientLight;
};
// update code called every frame
HotelShifter.prototype.update = function(dt) {
console.log(this.amibientLight.goCamera3w);
if(this.amibientLight.goCamera3w ===true){ //console.log(‘OtherScript’);
this.entityDetailHotel.enabled = false;}
};
Press ‘Roof top’ (where I have placed my reset camera function). Which is my foremost goal.
cf
function reset(){
//var appC = this.app; var entityCamera= new pc.Entity();
// this.entityCamera= appC.root.findByName(‘CameraMain’);
var self = this;
var posCam = new pc.Vec3(2.52, 0.266 , 1.598); var rotCam = new pc.Quat(4.81, 83.53,0); //this.entityCamera.setLocalPosition(this.posCam.x, this.posCam.y, this.posCam.z); //this.entityCamera.setLocalEulerAngles(this.rotCam);
self.setLocalPosition(posCam.x, posCam.y, posCam.z);
self.setLocalEulerAngles(rotCam);
On a side note, you are creating 3 entities for no reason as you are not using them?
var entityDetailHotel = new pc.Entity();
this.entityDetailHotel = this.app.root.findByName('Hotel_23');
var entityMinimHotel = new pc.Entity();
this.entityMinimHotel = this.app.root.findByName('Hotel_Minim');
var entityCamera = new pc.Entity();
this.entityCamera = this.app.root.findByName('CameraMain');
in both scripts, one can dive down into this entity’s scripts (+ vars) [to whome it may concern in Unity-land: it is not enough to have the ‘GameObject’ in hierarchy alone]
PS: on the other hand you might want to reconsider the thing about ‘new pc.Entity();’-lines as my system failed in relation to finding the objects unless it was the ‘two line setups’