Changing color of a childs text

I have an entity (a button) with a child entity (some text) attached. I want to change the colour of the childs text from within the main entities script. I tried:

myButton.children[0].element.color = new pc.Color(0,0,0);//black

But I get the error: Cannot set property ‘color’ of undefined

Am I doing something obviously wrong here? Is this not how to access child elements?

the easiest way to figure this out is to put a breakpoint there and inspect the objects in debugger.

Hi @Grimmy,

Without seeing the project it’s hard to say. What do you get in the console when if you log myButton.children ? I’ve run into issues before where, for whatever reason, there are empty entries in the children array.

Hi @Grimmy

Maybe you can find the text entity with something like

textEntity = buttonEntity.findByName('The name of the text entity');
textEntity.element.color = pc.Color.RED;
1 Like

The children are there but I just noticed some very strange things going on in the graph. Whenever I switch scenes the root node is duplicated like so… Is that normal???
image

Maybe this has something to do with it?..along with other issues Im having. I never noticed this until now.

This is because you are not deleting the Root entity when you change scenes, you are deleting the Scene entity which is a child below the Root

But I thought the new scene would be placed inside of the original root, not duplicated. Is there a way to transfer the new scene into the original root without creating a new root?

Remarkably that doesnt work either: textEntity is not defined

The code (inside the Brand_Select_Button))…

var textEntity;
textEntity= this.entity.findByName(“Text”);//get the child

even though the child (text) is right there in a child of this entity:

image

No, it is added to app.root. When loading a scene, you will always get the scene’s root entity added to it.

The easiest fix for your project (as you are loading multiple scenes in your project) is to change the name of the Root entity in the Init scene to something else.

Anywhere else you load a scene, instead of searching for ‘Scene’ as your old hierarchy, search for ‘Root’.

1 Like

I’d console.log(this.entity) to make sure it is the button entity.

OKay, that works.Thanks.

I still cant access the child entity though ?!?!

textEntity= this.entity.findByName(“Text”);//get the child - doesnt find anything

Thanks, I got it working. I think it was a scope issue in the end :blush:

how did you solve it?