How you can follow with Unity 5.x and Unity 2017

How you can follow with Unity 5.x and Unity 2018

Hello everybody!

If you follow the course with Unity 5, Unity 2017 or Unity 2018, you probably know our helpful document. Unfortunately, the formatting on Google Docs is fairly limited. So I thought I’m creating a little wiki here with a more pleasant formatting and additional information.


List of contents

2 Likes

GetComponent: Replacement for rigidbody2D and audio

As of Unity 5, most of the magical variables are deprecated. So if you want to get a component that is attached to a game object, you need to use GetComponent<T>(). Replace T by the class name.

Examples:

AudioSource audio = GetComponent<AudioSource>();
Rigidbody2D rigidbody2D = GetComponent<Rigidbody2D>();
MyScript myScript = GetComponent<MyScript>();

Additional information:

2 Likes

Import Settings of Images

When importing pictures that are going to be used as sprites, select “Sprite (2D and UI)” instead of “Texture” as Texture Type in the Import Settings.


Additional information:

1 Like

ParticleSystem

In Unity 5.5, the MainModule struct was introduced. It contains information like the startColor, startLifetime etc.

This is the replacement in Brick.cs:

void PuffSmoke() {
    ParticleSystem.MainModule main = smokePuff.GetComponent<ParticleSystem>().main;
    main.startColor = GetComponent<SpriteRenderer>().color;
}

Additional information:

1 Like

Loading new levels with the SceneManager

In Unity 5, the SceneManager was introduced. Previously, you simply called Application.LoadLevel. Levels are called scenes now.

This is what you need to do in your LevelManager.cs:

// Add the namespace at the top of your script
using UnityEngine.SceneManagement;

// Load the next scene from the build settings
public void LoadNextLevel () {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}

// Load a new scene by its name
public void LoadLevel (string name) {
    SceneManager.LoadScene(name);
}

Additional information:

1 Like

Replacement for OnLevelWasLoaded

With the SceneManagement, Unity introduced an event system. In comparison with the convenient OnLevelWasLoaded method, the new code probably looks overly complicated. Don’t worry too much about it. Copy and paste the code, and you are done. If you are interested in this topic (delegates and events), please refer to the additional information at the bottom.

Levels are now called scenes in Unity. Accordingly, I named the replacement for OnLevelWasLoaded OnSceneLoaded. It is a method created by me. You can name it whatever you want as long as the name is unique. Just make sure you add it and remove it from sceneLoaded. Furthermore, the method must take in two arguments: Scene scene, LoadSceneMode loadSceneMode.

OnEnable() and OnDisable() are Unity methods.

// Add the namespace at the top of your script
using UnityEngine.SceneManagement;

private void OnEnable () {
	SceneManager.sceneLoaded += OnSceneLoaded;  // subscribe
}

private void OnDisable () {
	SceneManager.sceneLoaded -= OnSceneLoaded;  //unsubscribe
}

private void OnSceneLoaded (Scene scene, LoadSceneMode loadSceneMode) {
	// the code that was used in OnLevelWasLoaded()
}

Additional information:

Sound

As of Unity 5, there aren’t any 2D/3D sounds anymore. The AudioSource now determines how sounds are treated. Go to the AudioSource component in your Inspector and change the Spatial Blend value.

Screenshot_147

This also applies to the PlayOneShot method which instantiates a short-living game object with an AudioSource attached. In the Block Breaker game, some crack sounds could suddenly appear louder than others. To fix this, do not pass on the position of the brick but of the position of the camera. The camera has got an AudioListener attached, so all AudioSources set to 3D use the distance to determine the volume of the sound clips.

AudioSource.PlayClipAtPoint(crack, Camera.main.position, 0.8f);

If you want to use Camera.main multiple times in your script, cache the reference for reasons of performance:

private Camera cam;

void Start() {
    this.cam = Camera.main;
}

void YourMethod() {
    AudioSource.PlayClipAtPoint(crack, this.cam.position, 0.8f);
}

Additional information:

Animations

Ben’s courgette is rotating smoothly but yours in Unity 5 or Unity 2017 is not? Select the keyframes in the Animation window, click your right mouse button and select ‘Auto’. If that does not work, try ‘Free Smooth’.

Screenshot_148


Additional information:

Privacy & Terms