Advanced Tutorial 5: How to Reference and Change a GameObject

From The Endless Mission Wiki
Jump to: navigation, search
Before following this tutorial module, make sure that you are starting with "Barebones Game" Loaded into the Editor. This tutorial will teach you how to manipulate the objects in your scene in a script, as well as the differences between Entities and GameObjects.

Introduction[edit | edit source]

Your scene in The Endless Mission is filled with assets called Entities. Using this Entity System allows The Endless Mission to save complex data about the object such as the EndlessEvents and properties that you have on it. Because these objects are saved as "Entities" you have to manually get a reference to the GameObject contained in the Entity. Doing this allows you to access and change all of the wonderful properties that GameObjects have to offer such as: Positional coordinates, rotation, visibility, behavior, pretty much anything you could want to do.

Getting the GameObject[edit | edit source]

1. In your "Barebones Game" scene, place an object anywhere.
2. Make and name a new script on this object.
3. Declare a variable of the Type EntityReference.
4. Add a [SerializeField] tag in the line above this variable to access it in the inspector. It should look like this:
 [SerializeField]
 private EntityReference myEntity;
5. Take the time now to drag the Entity you want to change into the new field in the inspector.
6. To get the GameObject of this Entity you can simply write the following inside of the Start() or Awake() functions:
 private GameObject myGameObject = myEntity.GameObject
7. From myGameObject you have access to tons of cool properties and variables attached to this Entity. For example, to change the position of the object:
 myGameObject.transform.position = new Vector3(0, 0, 0);
or disable the object in the scene:
 myGameObject.SetActive(false);

for a full list of the cool things you can do, check out: GameObjects .