Join Sue Blackman for an in-depth discussion in this video Using prefabs with arrays, part of Unity 3D 3.5 Essential Training.
Let's have a bit more fun with Prefabs. So far we've instantiated them in predictable places at predictable times. In our little game the player will start out in the staging area. It's small, and there's nothing of interest in it, but we might need to motivate him into leaving it. A bunch of rocks falling on him just might do the job. Now it would look pretty odd if all the rocks were the same size, but the Instantiate function doesn't have a Scale parameter. So the first thing we need to do is import the LotsOfRocks package from the right-click menu.
Import Package > Custom Package, and from our Assets folder we are going to bring in LotsOfRocks. In the Prefabs folder, you can see Rock1, Rock2, Rock3, and Rock4. I've added to each a Mesh collider set to Convex, a Physics material, a Rigidbody, and an Audio Source. Let's drag the four new rocks into the scene near the original rock pile, and then lift them up above the Rock Zone's collider.
Rock1, Rock2, Rock3, and Rock4, and I'll use Shift to select them all and drag them up a little ways. I'm going to deselect them, and click Play, and watch them fall. We'll exit Play mode. And now we need to update each Rock's Prefab by clicking Apply in the selector to make sure it knows where the new location is.
Apply, Apply, Apply, and Apply, and now we can delete the rocks in our scene, Delete, and there we go, so far so good. Let's make a new Rock script in the Game Scripts folder by duplicating the original. So I'll select it and use Ctrl+D, and I want to name this one RockDropper.
This is what tells us it's going to be an array. Next we need to change the instantiate line. To access an element of an array we need to put the element number in brackets. So Prefabs and open bracket, I'm going to start with element 0 and close bracket. Arrays always start with element 0. Let's save the script now and get back to the editor. We can replace the original script with this one on the Rock Zone object.
And we can do that to the browser, and we'll choose RockDropper. If you click Play, and use the S key to back into the collider, you'll get an IndexOutOfRangeException Error on the console. Let's look at that. That's because we haven't filled out the array with anything yet, and then we're trying to instantiate element 0. So let's exit Play mode and fix that.
I'll close the console, exit Play mode. And in the Rock Zone, we need to open up Prefabs, set its size to four. And drag the Rock into each parameter. Now we can click Play, press S to back into the zone, and we get lots and lots of rocks.
Let's stop before we get too many. So this time the Rock Prefab from element 0 drops into the scene. And it keeps dropping because each rock triggers the zone on entry. In this case that's a serendipitous side effect. It will eventually shut itself off when it fills up. The problem now is that the same rock is being used over and over. We'll fix that in the next video.
- Understanding game and level design theory
- Organizing your project in Unity
- Creating and transforming objects
- Setting up the geometry
- Painting in terrain, textures, and trees
- Adjusting the render settings
- Importing terrains
- Creating a first-person controller
- Creating materials and shaders
- Lighting the game
- Working with cameras and multiple views
- Animating characters and assets
- Creating fire with particle systems
- Managing the GUI (graphical user interface)
Skill Level Beginner
Creating Urban Game Environments in 3ds Maxwith Adam Crespi5h 54m Intermediate
1. Exploring Unity 3D
2. Exploring the Terrain Editor
3. Creating the Environment
Publishing project settings3m 37s
4. Introducing Unity Scripting
5. In-Game Scripting
6. Working with GameObjects and Components
7. Exploring Prefabs
8. Using Imported Assets
9. Understanding Lighting
10. Keyframing Animation
11. Animating Skinned Meshes and Controlling Characters
12. Working with Cameras and Layers
13. Creating Game GUIs
14. Extra Techniques and Features
What's next1m 13s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.