Review homework
Udemy course
Spheres array demo
Vectors, perhaps
Introduce keyboard race demo
How did it go?
He drags in a component from the same GameObject to set the reference.
This is the exact same thing as using GetComponent!
You don't have to delete the using statements!
They're not hurting anyone!
The password is: archive.
You need this for the next homework!
Take a couple of minutes and try to access it now.
sound harder than they are
A Scriptable Object is a special Unity structure that is a template for data objects.
You can think of it as the master copy of a form – one that you keep blank so you can make more copies when you need them.
Scriptable Objects are mostly intended to store variables, but you can also write functions in them.
A Scriptable Object is a special Unity structure that is a template for data objects.
You can think of it as the master copy of a form – one that you keep blank so you can make more copies when you need them.
Scriptable Objects are mostly intended to store variables, but you can also write functions in them.
Games involve a lot of information.
This information has to live somewhere.
Some things about your game can be represented in the scene file.
For example, you might have a scene for each level of a platformer like Thomas Was Alone.
But what about a strategy game like Civilization?
Many game teams manage this information outside the game engine, using spreadsheets, text documents, and so on.
image credits: "The VGLC: The Video Game Level Corpus"
image credit: Ruby Cow Games
Pros:
Very flexible.
Cons:
The designer has to understand the format of the document, i.e. what information goes in what column.
And the engineer has to write code to get and read the file. Unity does not do this automatically.
Scriptable Objects are meant to be a kind of in-between option that lets the designers do their work directly in Unity.
In the homework you will see two examples of the kind of information you might store using Scriptable Objects:
Another way you might use them is like a table in a Word document. For example, to store how many points different things are worth.
image credit: classicgaming.cc
The basic steps for creating ScriptableObjects are:
[CreateAssetMenu]
How would you use Scriptable Objects to represent information in
Here's one example, an RPG weapon:
An array is like what humans would call a list of objects. (To computers, a List is a different thing.)
An array can only contain one kind of value, that is, strings, floats, GameObjects, and so on.
Sometimes there is a set of things you care about as a group:
GameObject card1;
GameObject card2;
GameObject card3;
Here is what not to do:
Instead, if you want to store a set of things together, you can create an array variable to hold them. That looks like this:
GameObject[] cards;
string[] studentNames; InventoryItem[] playerInventory;
GameObject[] cards;
For the most part, if you are working with an array you will make it visible in the Inspector, like in the homework:
[SerializeField] GameObject[] cards;
Then you can just drag in what you need.
Later you will learn about List, which is a more flexible version of an array. We will return to Lists soon.
You can only get to an item inside an array via its position, or index.
Arrays start counting at zero, which is confusing for humans:
studentNames = new string[3]; studentNames[0] = "Sabrina"; studentNames[1] = "Sandy"; studentNames[2] = "Sriram";
Even more confusingly, when you give a length for the array you don't start at zero but at one.
So this code says there are three items in the array, but the last item in the array has index 2.
studentNames = new string[3]; studentNames[0] = "Sabrina"; studentNames[1] = "Sandy"; studentNames[2] = "Sriram";
The first index (position) in an array is always zero.
studentNames[0] //"Sabrina"
The last position in an array is more complicated.
It is the number of items in the array (its length) minus one, because of starting at zero.
studentNames.Length - 1
So the last thing in your array is here:
studentNames[ studentNames.Length - 1 ]
A loop visiting each element in your array will therefore count from 0 to (length-1).
for (int i=0; i < studentNames.Length - 1; i++) { print(studentNames[i]); }
If you try to get to a position that isn't actually in the array, you will get a particular kind of error:
studentNames = new string[3]; studentNames[0] = "Sabrina"; studentNames[1] = "Sandy"; studentNames[2] = "Sriram"; studentNames[3] = "oops";
If you forget to initialize your array you'll get an error like this.
There are lots of other ways to get this error, but if you see that this error comes from a line of code using an array, then you probably forgot to initialize.