GettingTheEntity.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Xenko.Core.Mathematics;
  2. using Xenko.Engine;
  3. namespace Tutorials.Basics {
  4. /// <summary>
  5. /// This script demonstrates how to access the entity where the script is attached to.
  6. /// We also learn how to access a parent of our entity and how to check if that entity exists.
  7. /// </summary>
  8. public class GettingTheEntity : SyncScript {
  9. string name = "";
  10. string parentName = "";
  11. //Executes only once, at the start of the game
  12. public override void Start() {
  13. //We store the name of the Entity that we are attached to
  14. name = Entity.Name;
  15. //We retrieve the parent entity by using the GetParent() command.
  16. Entity parentEntity = Entity.GetParent();
  17. //It is possible that our entity does not have a parent. We therefor check if the parent is not null.
  18. if (parentEntity != null) {
  19. //We store the name of our Parent entity
  20. parentName = parentEntity.Name;
  21. }
  22. //The above code can be shortened to 1 line by using the '?' operator
  23. parentName = Entity.GetParent()?.Name;
  24. }
  25. //Updates every frame
  26. public override void Update() {
  27. //Using the 'DebugText.Print' command, we can quickly print information to the screen
  28. DebugText.TextColor = Color.Red;
  29. DebugText.Print(parentName, new Int2(10, 20));
  30. DebugText.Print(name, new Int2(30, 40));
  31. }
  32. }
  33. }