Skip to content

Latest commit

 

History

History
98 lines (73 loc) · 2.38 KB

scene-objects.md

File metadata and controls

98 lines (73 loc) · 2.38 KB

Accessing scene objects

One of the most important aspects of the scripts is interaction and accessing other scene objects including of course the actor that script is attached to. For instance, if game wants to spawn a ball in front of the player, it needs to get a player location and a view direction which can be done by using Flax.

Accessing actors

Every script has inherited property Actor that represents an actor the script is attached to. For example you can use it to modify the actor position every frame:

public override void OnFixedUpdate()
{
	Actor.Position += new Vector3(0, 2, 0);
}

You can also print its name:

Debug.Log(Actor.Name);

See Actor class reference to learn more.

You can also print all child actors and rotate the parent actor:

public override void OnStart()
{
    // Prints all children names
    var children = Actor.GetChildren();
    foreach (var a in children)
        Debug.Log(a.Name);

    // Changes the child point light color (if has)
    var pointLight = Actor.GetChild<PointLight>();
    if (pointLight)
        pointLight.Color = Color.Red;
}

public override void OnFixedUpdate()
{
    // Rotates the parent object
    Actor.Parent.LocalEulerAngles += new Vector3(0, 2, 0);
}

Accessing other scripts

Scripts attached to the actors can be queries like the actors using a dedicated methods:

private void OnTriggerEnter(Collider collider)
{
    // Deal damage to the player when enters the trigger
    var player = collider.GetScript<Player>();
    if (player)
        player.DealDamage(10);
}

You can also query all the scripts of the any actor and use them to perform any action:

private void OnTriggerEnter(Collider collider)
{
    foreach (var script in collider.Scripts)
    {
        if (script is IAdProvider provider)
            provider.ShowAd();
    }
}

Finding actors

Flax implements API to find objects.

private void OnTriggerLeave(Collider collider)
{
    var obj = Actor.Scene.FindActor("Spaceship");
    Destroy(obj);
}

However, in most cases, the best solution is to expose a field with reference to the object and set it in the editor to improve game performance.

public Actor Spaceship;

private void OnTriggerLeave(Collider collider)
{
    Destroy(ref Spaceship);
}