Basic Variable Communication

Hi, I know this must seem very basic but how do I declare variables that are NOT exposed in the editor and how do I communicate with them from other scripts. I thought

Entity A
var myTest=42;//declared in root of script

Entity B“Entity A”);

…but that just prints undefined.


Hi @Grimmy,

You’re running into issues of scope here. Since you are declaring that variable globally, you would just use myTest as it will be the same across all scripts and entities loaded in the browser. If you wanted to be specific to each entity, you would want to declare it within the scope of the script instance. The way you would access it would be like so:

On your first entity:

var ExampleA = pc.createScript('exampleA');

ExampleA.prototype.initialize = function() {
	this.example1 = 42;

Then the second would look something like this:

var ExampleB = pc.createScript('exampleB');

ExampleB.prototype.initialize = function() {
	this.exampleAEntity ='Example A');

	var whatsExample1 = this.exampleAEntity.script.exampleA.example1;

	console.log(whatsExample1); // 42

Perfect.Thanks for the explanation!!

1 Like