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:
You can also print its name:
See Actor class reference to learn more.
You can also print all child actors and rotate the parent actor:
using FlaxEngine;
public class MyScript : Script
{
public override void OnStart()
{
// Prints all children names
var children = Actor.Children;
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 Float3(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 provider in collider.GetScripts<IAdProvider>())
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.