C# Addendum

This was another very straightforward port to C#. The only thing that could potentially trip you up is realizing that in C# version of the FallingObject script, the built-in array's Length property starts with a capital L, while Unity JavaScript uses a lowercase l.

Here's the code:

FallingObjectCSharp.cs

using UnityEngine; using System.Collections; public class FallingObjectCSharp : MonoBehaviour { public GameObject prefab; public int speed; public AudioClip[] audioClips; void Update () { transform.position = new Vector3(transform.position.x, transform.position.y - speed * Time.deltaTime, transform.position.z); if(transform.position.y < 0) { audio.PlayOneShot(audioClips[Random.Range(0,audioClips.Length)]); Instantiate(prefab, transform.position, ...

Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.