🔍 getComponentFromObject

What is it?

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.

📖 Syntax

const comp = getComponentFromObject("ObjectName", "Type");

✅ Basic Example

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
}

✅ Another Example

Accessing the PlatformMover component of a platform named MovingPlatform:

const mover = getComponentFromObject("MovingPlatform", "PlatformMover");
if (mover) {
  mover.speed = 150;
}

📌 Notes

- Returns null if the object does not exist or does not have the requested component.
- Useful when you need to modify a component of an object you know by name without storing a reference beforehand.
- Internally it is equivalent to:
const obj = getObjectByName("Player");
const comp = getComponent(obj, "AudioSource");