[SOLVED] Disappearing Text Entities

Hi! I’m suppose to add a counter to my game, but I can’t get passed making the variables for my entities work since they keep disappearing despite having the “right” code.

This is what my game looks like at the moment as the text for the supposed entity counters have vanished :frowning:

var GameBarScript = pc.createScript('gameBarScript');

GameBarScript.attributes.add('itemTxt', {type: 'entity'});
GameBarScript.attributes.add('levelTxt', {type: 'entity'});
GameBarScript.attributes.add('timerTxt', {type: 'entity'});

var itemCount = 0;
var level = 1;
var timer = 10;

// initialize code called once per entity
GameBarScript.prototype.initialize = function() {
    this.itemTxt.element.text = itemCount;
    this.levelTxt.element.text = level;
    this.timerTxt.element.text = timer;
};

// update code called every frame
GameBarScript.prototype.update = function(dt) {

};

Hello @renee.aguila28 and welcome! :wave:

The text field accept string value. So if you are trying to assign them any other datatype please make sure to add .toString() at the end which will solve the problem.

// initialize code called once per entity
GameBarScript.prototype.initialize = function() {
    this.itemTxt.element.text = itemCount.toString();
    this.levelTxt.element.text = level.toString();
    this.timerTxt.element.text = timer.toString();
};

Hello! Thanks for the reply!

I’ve tried adding the .toString() but it doesn’t seem to work. I’ve tried refreshing and parsing but to no avail :frowning:

var GameBarScript = pc.createScript('gameBarScript');

GameBarScript.attributes.add('itemTxt', {type: 'entity'});
GameBarScript.attributes.add('levelTxt', {type: 'entity'});
GameBarScript.attributes.add('timerTxt', {type: 'entity'});

var itemCount = 0;
var level = 1;
var timer = 10;

// initialize code called once per entity
GameBarScript.prototype.initialize = function() {
    this.itemTxt.element.text = itemCount.toString();
    this.levelTxt.element.text = level.toString();
    this.timerTxt.element.text = timer.toString();
};

// update code called every frame
GameBarScript.prototype.update = function(dt) {

};

Ok :thinking:

Can you share screenshot of where the script is added? I mean did you drag and dropped those entities there? Is this script enabled?
Also is that entity enabled where this GameBarScript is attached?

Screenshot will be helpful in case

Here’s a screenshot! I made sure that the entities are attached and the script is enabled.

Can you please show me screenshot of itemTxt entity selected?
Does it have font? Font size is good enough? Is it placed properly?
Try adding random text into itemTxt entity which should visible there…

Here’s a screenshot with the itemTxt entity selected. It does have a font, decent font size and its placed properly. I’ve also added random text.

Try adding numbers in that text field…
If does not work then
Is this possible for you share project link? So can take a look…

Here’s the project link
https://playcanvas.com/editor/scene/1914307

1 Like

@renee.aguila28 It seems like there is problem with font where it is not showing any numbers…

You need to change the font to make those numbers work…
It will solve the problem. :slightly_smiling_face:

it finally worked!! thank you so much for the help!!!

1 Like