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.
Let's open it in the Script editor and make a few changes, double-click, make sure we're on RockDropper. And the first change we are going to make is the variable. Instead of 1 of type transform we're going to make it an array. Unity uses two types of arrays, the usual Javascript type array, and its own version. The Unity array must be declared as a particular type, and it can show up in the Inspector where we can change its size and load up its elements.
A Javascript array doesn't store types causing the #pragma strict to report an error. With Unity's emphasis on mobile we're better off to stick to the Unity arrays. If you're already familiar with Javascript arrays you may want to read through the scripting reference on arrays to see the differences. So in the RockDropper script, let's change the variable line to their Prefabs, I'm adding an S, and then transform and then open and shut bracket.
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.
Author
Released
7/19/2012- 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
- Building projectiles with JavaScript
- 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
Duration
Views
Related Courses
-
Game Character Creation in Maya
with Chris Reilly2h 58m Intermediate
-
Introduction
-
Welcome55s
-
Unity basics1m 8s
-
Using the exercise files1m 29s
-
-
1. Exploring Unity 3D
-
Exploring the interface4m 28s
-
Organizing your project8m 12s
-
Exploring Scene view3m 14s
-
Navigating Scene view2m 52s
-
Creating objects7m 38s
-
Transforming objects8m 16s
-
-
2. Exploring the Terrain Editor
-
Painting the topography3m 56s
-
Painting trees and forests4m 56s
-
Painting detail meshes5m 4s
-
Adjusting terrain settings3m 23s
-
3. Creating the Environment
-
Publishing project settings3m 37s
-
4. Introducing Unity Scripting
-
Exploring Unity scripting3m 34s
-
Using variables11m 46s
-
Discovering functions4m 10s
-
Looking at function syntax3m 44s
-
Printing to the console8m 28s
-
Scripting basic functions7m 12s
-
Combining transforms5m 27s
-
Setting transforms8m 34s
-
-
5. In-Game Scripting
-
Raycasting6m 56s
-
Building timers7m 8s
-
Creating a simple projectile13m 16s
-
Refining the projectile5m 32s
-
Sending messages3m 38s
-
Reacting to messages4m 50s
-
Juggling Play and Edit modes7m 33s
-
Suppressing input8m 2s
-
6. Working with GameObjects and Components
-
Creating GameObjects6m 14s
-
Understanding components5m 3s
-
Using colliders for barriers7m 15s
-
Using colliders for triggers4m 28s
-
Exploring physics7m 15s
-
Making cloth9m 56s
-
Working with wind zones7m 50s
-
Using an audio source6m 14s
-
Creating a sound zone5m 34s
-
Adding audio effects6m 31s
-
-
7. Exploring Prefabs
-
Creating and reusing prefabs8m 11s
-
Using prefabs with arrays5m 28s
-
Creating particle systems6m 58s
-
Refining particle systems5m 30s
-
Combining particle systems7m 10s
-
Upgrading weapons6m 15s
-
Exploring water effects7m 53s
-
-
8. Using Imported Assets
-
Importing static objects12m 36s
-
Manipulating textures5m 42s
-
Handling multiple materials5m 42s
-
Animating UVs8m 27s
-
Tracking objects with LookAt5m 45s
-
-
9. Understanding Lighting
-
Investigating ambient light2m 21s
-
Looking at light types3m 13s
-
Exploring shadows5m 23s
-
Baking lighting with Beast8m 16s
-
Experimenting with Beast7m 42s
-
Baking the game scene4m 20s
-
Creating lighting effects6m 24s
-
Adding a flashlight9m 20s
-
-
10. Keyframing Animation
-
Using the Animation view9m 53s
-
Animating transparency7m 12s
-
Keyframing events6m 28s
-
Avoiding keyframing pitfalls5m 27s
-
11. Animating Skinned Meshes and Controlling Characters
-
Importing skinned meshes9m 42s
-
Dynamic parenting6m 44s
-
-
12. Working with Cameras and Layers
-
Introducing cameras4m 53s
-
Managing multiple levels7m 34s
-
-
13. Creating Game GUIs
-
Working with the Unity GUI10m 31s
-
Using GUI skins6m 30s
-
Introducing fonts and styles5m 56s
-
Scripting in-game menus3m 44s
-
Hooking up an in-game menu5m 20s
-
Creating custom cursors8m 42s
-
-
14. Extra Techniques and Features
-
Creating fade transitions5m 34s
-
Setting AI pathfinding9m 56s
-
-
Conclusion
-
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.
CancelTake 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.
Share this video
Embed this video
Video: Using prefabs with arrays