Here’s an overview of a way to add Level 2 to the Asteroids example. The complete project package is here.
I’ve updated Level 1 a bit, since last time, the main differences being script & scene clean up & adding a background image to our scene. For example, instead of a win scene and a lose scene, consolidated those into the endGame scene (see screenshot below).
For an obvious visual difference between Level 1 and Level 2, I decided to swap the background graphic by updating the Material of a new GameObject called bgImg1 that holds the background image.
Create your background graphics in Photoshop, etc and drag & drop them into the Project panel. Create 2 new Materials and assign the background graphics as the textures for those materials (one for each).
playerScript.js, relevant lines:
var mats:Material; var bgImg1:GameObject; ... bgImg1 = GameObject.Find('BgImg1'); //set background Material based on Game Level (mats is zero based, levels start with 1): bgImg1.renderer.material = mats[ mainMenuScript.gameLevel-1 ];
The mats array is first defined as a public variable in playerScript.js, which leads to it being visible in the Inspector. To assign correct Materials (w/ texture) to mats array:
1. select Player obj in Hierarchy
2. find Player’s Script component in Inspector
3. fill in Mats values – drag & drop each Material from Project to the Mats array in Inspector
IndexOutOfRangeException and UnassignedReferenceException errors for duplicated scene
In this example, I’m reusing as many assets as possible from Level 1 to Level 2. One issue that came up when reusing the Player GameObject and it’s playerScript.js script component was the IndexOutOfRangeException error for the mats array: IndexOutOfRangeException: Array index is out of range. playerScript.Start () (at Assets/Scripts/playerScript.js:82). I got this error because when I duplicated level1 scene to create level2, Unity didn’t automatically repopulate the mats array for me. I had to switch to level2, go into the Inspector for Player and manually repopulate again. This fixed the error. If you get “UnassignedReferenceException: The variable explosion of ‘playerScript’ has not been assigned.” for bullet or explosion vars from playerScript.js, it’s a similar issue – you need to manually reassign the prefabs for bullet and explosion in the Inspector.