Scripting Flashcards
What can you do to check whether a script is running or not when the game is playing?
You can use the command Debug.Log(“a string”);This command writes to the Unity console.
What do you have to do to make a script variable visible in the inspector?
You have to make it a public variable
How do you get access to a GameObject’s components using a script
You use the GetComponent function. For instance, to get the rigidbody of a GameObject:void Start { Rigidbody rb = GetComponent[Rigidbody>();}
Do you have to use the GetComponent function if you want to change the transform of a GameComponent?
No, Unity provides built-in variables for very commonly used components, like transform:void Start { transform.position = Vector3.zero;}
How can you make a script access other GameObjects?
There are several ways. The most straightforward is to make a public GameObject variable, e.g. ‘public GameObject player;’Your script-GameObject will now have a variable in the script component (in the inspector). You have to drag the actual GameObject from the hierarchy to the variable to make it work.Another way to do it is to make the public GameObject variable, and then use GameObject.Find(“name”) to find an individual GameObject by name, or GameObject.FindWithTag(“object tag”) to find by tag.
When is the Start function called?When is the Update function called?
The Start function is called just before the object’s first frame update.The Update function is called before every frame update.
When is the FixedUpdate function called?
The FixedUpdate function is called just before every physics update. Physics updates and frame updates do not occur with the same frequency. Because of this, you will get more accurate results if you put physics code in FixedUpdate rather than Update.
When is LateUpdate called, and what is it used for?
LateUpdate is called after Update and FixedUpdate, and is useful for when you need to change things after everything else is changed (in Update/FixedUpdate). E.g. when the adjustment to the camera’s orientation has to be made after the target object has moved.
What is the difference between the Start and the Awake functions?
Start is called before the first frame or physics update on an object, while the Awake function is called for each object in the scene at the time when the scene loads. Although the various objects’ Start and Awake functions are called in arbitrary order, all the Awakes will have finished before the first Start is called.This means that code in a Start function can make use of other initializations previously carried out in the Awake phase.
What is Rigidbody?
Adding a Rigidbody component to an object will put its motion under the control of Unity’s physics engine. Even without adding any code, a Rigidbody object will be pulled downward by gravity and will react to collisions of incoming objects if the right Collider component is also present.
What is important to remember about Rigidbodies and model/sprite size?
A common problem when starting out with Rigidbodies is that the game physics appears to run in “slow motion”. This is actually due to the scale used for your models. The default gravity settings assume that one world unit corresponds to one metre of distance. With non-physical games, it doesn’t make much difference if your models are all 100 units long but when using physics, they will be treated as very large objects. If a large scale is used for objects that are supposed to be small, they will appear to fall very slowly - the physics engine thinks they are very large objects falling over very large distances.
When is OnCollisionEnter/OnCollisionEnter2D called?
It’s called when this collider/rigidbody has begun touching another collider/rigidbody.
When is OnTriggerEnter called?
This message is sent to the trigger collider and the rigidbody (if any) that the trigger collider belongs to, and to the rigidbody (or the collider if there is no rigidbody) that touches the trigger. Note that trigger events are only sent if one of the colliders also has a rigidbody attached.
What is the Instantiate function?
In Unity, a GameObject can be created using the Instantiate function which makes a new copy of an existing object. If you are cloning a GameObject then you can also optionally specify its position and rotation. Note that the object from which the copy is made doesn’t have to be present in the scene. It is more common to use a prefab dragged to a public variable from the Project panel in the editor. Also, instantiating a GameObject will copy all the Components present on the original.
What is the Destroy function?
The Destroy function can destroy an object after the frame update or optionally after a short time delay. Note that the Destroy function can destroy individual components without affecting the GameObject itself. A common mistake is to write something like:Destroy(this);which will actually just destroy the script component that calls it rather than destroying the GameObject the script is attached to.