getComponentFromObject is an API function that combines
getObjectByName and getComponent into a single call.
It allows you to look up an object in the scene by name and directly return
one of its components.
const comp = getComponentFromObject("ObjectName", "Type");
"AudioSource").null if not found).Suppose you have an object named Player with an AudioSource:
{
"type": "player",
"name": "Player",
"x": 100,
"y": 200,
"components": [
{
"type": "AudioSource",
"clip": "jump.wav",
"volume": 1.0,
"loop": false
}
]
}
You can access the component from any script like this:
const audio = getComponentFromObject("Player", "AudioSource");
if (audio) {
audio.playing = true; // play jump sound
}
Accessing the PlatformMover component of a platform named MovingPlatform:
const mover = getComponentFromObject("MovingPlatform", "PlatformMover");
if (mover) {
mover.speed = 150;
}
null if the object does not exist or does not have the requested component.const obj = getObjectByName("Player");
const comp = getComponent(obj, "AudioSource");