Home
Username:
Password:
Introduction to Game Development Using Unity 3D Tutorials

Introduction to Scripting / Private Versus Public

Subtitles of the Movie

Finishing up our look into Unity Scripting and basically the power underneath the hood, we will look now at Private Versus Public variables. Now, this sounds very complicated but it's in fact, really easy. When I open up my Respawn Script, as you can see, I can add variables to the top of the script or basically, outside of a function and call them for example, myVar equals one. Now, if I would add another variable inside of the Update function and type for example, var myFunctionVar equals one. And I save this and go back into Unity. Click on the player which have the script attached to it. You could see down here, that MyVar is added as a tweakable variable in the editor. So basically, when I change this variable to for example, two, I open up back my script. It does not change what I've said in scripting, but however, when my game runs it is set to two, instead. This allows you for very fast tweaking of the variables in your script. And this is what makes Unity such a productive tool, because one of the things you do most in designing games is tweaking your variables. But notice also, that the variable that I have defined inside of the function does not show up in the editor. Because it's inside of the function and it will stay inside of that function. Let me get rid of this variable here. Now also, sometimes you want variables to be accessible in your script outside of function, so you define them here. But you do not want them to show up in the editor. So what you do to do this is, type private mySecondVar equals one. Save the script, go back in to Unity and as you can see, it does not show the second variable because you've set it to private. So by default, a variable is public and you can tweak it inside at the editor. But sometimes, you can add a private var. I've added a little error, you have to type var, as well. Let's go back and as you can see, I had a little error here, which is now fixed. OK. Now last but not the least, there's also a possibility to add static vars, static var myStaticVar equals true, for example. And static vars are accessible outside of your script by other script. So this allows multiple scripts to talk with each other. This is extremely powerful to example, send commands from one script to another, or for one character to another. It's very interesting stuff. So let's get into making more of our game and next let's look in to what exactly 3D is and how it works and how we can manipulate things.

Tutorial Information

Course: Introduction to Game Development Using Unity 3D
Author: Efraim Meulenberg
SKU: 34092
ISBN: 1-936334-03-8
Release Date: 2010-02-23
Duration: 5.5 hrs / 75 lessons
Work Files: Yes
Captions: Available on CD and Online University
Compatibility: Vista/XP/2000, OS X, Linux
QuickTime 7, Flash 8

VTC Sign up & Benefits

  • Unlimited Access
  • 98,729 Video Tutorials (23,265 free)
  • Video Available as Flash or QuickTime
  • Over 1026 Courses
  • $30 for One Month Access
  • Multi-User Discounts Available