| Scripting Basic Knowledge | 
This page contains some basic advices on scripting. You also can check out Samples and Tutorials.
 Contents
Contents How to perform action right after scene loading
How to perform action right after scene loadingRead Event Handlers page with simple scripting examples.
 How to write to the log
How to write to the logIn order to write messages to the log and to display log messages in the console of the Player, following methods are used:
Log.Info(string message); Log.Warning(string message);

 How to gain access to the scene object
How to gain access to the scene objectvar scene = Scene.First;
or better way to get by another component of your scene
var scene = component.FindParent<Scene>();
 How to find component by the name
How to find component by the namevar scene = Scene.First;
var obj = scene.GetComponent("MyObject"); How to call C# script method
How to call C# script methodvar scene = Scene.First;
var result = scene.GetComponent("C# Script 2").MethodInvoke("Method", 2, 3);
Log.Info(result); How to call method or property of any object by name
How to call method or property of any object by namevar result = obj.MethodInvoke("MyMethod", 2, 3);
Log.Info(result);
obj.PropertySet("MyProperty", 5); 
var value = obj.PropertyGet("MyProperty"); How to create component
How to create componentvar obj = scene.CreateComponent<Component>(enabled: false); ... // Before enabling, configure the object parameters. obj.Enabled = true;
 How to destroy component
How to destroy componentTo delete an object, it must be detached from the object hierarchy. After this action, the object will be completely turned off.
The first way to delete is using RemoveFromParent method which supports a queue of deleted objects. Using a queue allows you to avoid errors in cases when the list of objects is changed while working with it.
obj.RemoveFromParent( true );
You can also delete an object using the Dispose method, although in this case the object cannot be used anymore.
obj.Dispose();
 How to add property to any component from code
How to add property to any component from codeYou can add a property directly in the editor.
// Add.
var property = scene.CreateComponent<VirtualProperty>(enabled: false);
property.Name = "MyProperty";
property.Type = MetadataManager.GetTypeOfNetType(typeof(double));
property.Enabled = true;
// Set, get.
scene.PropertySet("MyProperty", 5);
var value = obj.PropertyGet("MyProperty");
Log.Warning(value); How to place 3D model in the scene
How to place 3D model in the scene// Create mesh in space object without enabling. var meshInSpace = scene.CreateComponent<MeshInSpace>(enabled: false); // Set transform. meshInSpace.Transform = new Transform(new Vector3(2, 1, 2), new Angles(0, 0, 45)); // Or set transform by the auxiliary methods (SetPosition, SetRotation, SetScale, LookAt). meshInSpace.SetPosition(2, 1, 2); meshInSpace.SetRotation(0, 0, 45); // Set 3D model. var resourceName = @"Samples\Starter Content\Models\Sci-fi Drone\Sci-fi Drone.fbx"; var meshNameInsideResource = "$Mesh"; meshInSpace.Mesh = ReferenceUtility.MakeReference(resourceName + "|" + meshNameInsideResource); // Enable the object in the scene. meshInSpace.Enabled = true;

 How to place prepared scene object in the scene
How to place prepared scene object in the scene// Get object type. var resourceName = @"Samples\Starter Content\Scene objects\Sci-fi Box\Box type.scene"; var objectNameInsideResource = "$Box"; var boxType = MetadataManager.GetType(resourceName + "|" + objectNameInsideResource); // Create the object without enabling. var box = (ObjectInSpace)scene.CreateComponent(boxType, enabled: false); // Set initial position. // If object contains physical body, then need change transform of the body instead of updating the object itself. box.GetComponent<RigidBody>().Transform = new Transform(new Vector3(2, 4, 3), new Angles(0, 0, -45)); // Or set transform by the auxiliary methods (SetPosition, SetRotation, SetScale, LookAt). box.SetPosition(2, 4, 3); box.SetRotation(0, 0, -45); // Enable the object in the scene. box.Enabled = true;

 See also
See also