that are part of C The main class is MainPlayer and it is a subclass of

That are part of c the main class is mainplayer and

This preview shows page 6 - 7 out of 13 pages.

collection data structures (ArrayList, Stack, Queue, HashTable, etc.) that are part of C#.The main class isMainPlayer, and it is a subclass ofMonoBehaviour. All script objects in Unityare subclasses ofMonoBehaviour.Many script class objects will involve: (1) some sort of initialization and (2) some sort ofincremental updating just prior to each refresh cycle (when the next image is drawn to thedisplay). The template facilitates this by providing you with two blank functions,StartandUpdate, for you to fill in. Of course, there is no requirement to do so. For example, your scriptmay require no explicit initializations (and thus there is no need forStart), or rather thanbeing updated with each refresh cycle, your script may be updated in response to specificevents such as collisions (and so there would be no need forUpdate).Awake versus Start:There are two Unity functions for running initializations for your gameobjects,Start(as shown above) andAwake. Both functions are called at most once for anygame object.Awakewill be called first, and is called as soon as the object has been initializedby Unity. However, the object might not yet appear in your scene because it has not beenenabled. As soon as your object is enabled, theStartis called.Let us digress a moment to discuss enabling/disabling objects.Unity game objects canbe “turned-on” or “turned-off” in two different ways (without actually deleting them).Inparticular, objects can beenabledordisabled, and objects can beactiveorinactive. (Eachgame object in Unity has two fields,enabledandactive, which can be set to true or false.) Thedifference is that disabling an object stops it from being rendered or updated, but it does notdisable other components, such as colliders. In contrast, making an objectinactivestops allits components.For example, suppose that some character in your game is spawned only after a particularevent takes place (you cast a magic spell). The object can initially be declared to bedisabled,and later when the spawn event occurs, you ask Unity to enable it.Awakewill be called onthis object as soon as the game starts.Startwill be called as soon as the object is enabled. Ifyou later disable and object and re-enable it,Startwill notbe called again. (Both functionsare called at most once.)To make your life simple, it is almost always adequate to use just theStartfunction for one-time initializations. If there are any initializations thatmustbe performed just as the gameis starting, thenAwakeis one to use.Controlling Animation Times:As mentioned above, theUpdatefunction is called with eachupdate-cycle of your game. This typically means that every time your scene is redrawn, thisfunction is called.Redraw functions usually happen very quickly (e.g., 30–100 times persecond), but they can happen more slowly if the scene is very complicated. The problem thatthis causes is that it affects the speed that objects appear to move. For example, supposethat you have a collection of objects that spin around with constant speed. With each call toUpdate, you rotate them by 3. Now, ifUpdate
Background image
Image of page 7

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture