Display variable on screen

Hello can anybody tell me how to display variable as text on the screen? I mean something like “Coins picked: (variable)”

Hi @smokys,

You can easily do that with a text UI element and some simple code. Check the UI example projects on how to set this up:

https://developer.playcanvas.com/en/tutorials/?tags=ui

1 Like

Im trying it and it seem to be complicated, im little bit frustrated but i will try to work with it somehow.

The closest tutorial that will help you is probably this one https://developer.playcanvas.com/en/tutorials/ui-elements-stats-counter/

Where the text element is updated to show a particular value.

2 Likes

An easy way to display a variable in a UI is by doing this:


myscript.prototype.initialize = function(){
   this.coinvalue = 0;
}

myscript.prototype.update = function(dt){
    var textbox = this.app.root.findByName('TextBoxName');
    var value = this.coinvalue;
    
    textbox.element.text = value;
}
2 Likes

This one helped waaaay tooo enough, thank you so much <3

This is one of the really easiest

1 Like

there is exatcly 99 coins :o

2 Likes

Haha ye, funny is that it is all random :smiley:

if you want to switch the perspective to 2d just switch this to orthographic ( its under the camera )


Screenshot 2020-11-01 at 3.21.19 PM

1 Like
myscript.prototype.initialize = function(){
   this.coinvalue = 0;
}

myscript.prototype.update = function(dt){
    var textbox = this.app.root.findByName('TextBoxName');
    var value = this.coinvalue;
    
    textbox.element.text = "Coins Picked:" + value;
}

modified it a bit. it will now show Coins Picked next to the updating value.

4 Likes

Well done, that is what i was looking for :slight_smile:

1 Like