Write your own spawner class
Tutorial
·
Beginner
·
+10XP
·
30 mins
·
(111)
Unity Technologies

In this tutorial, you’ll:
- Identify the purpose of your class
- Write the class to make your code more efficient
- Add and review a special function for your class
- Initialize variables for your class
- Check your script changes in the Console
When you’ve finished, you’ll have created your own potion spawning class for the Creator Kit: Beginner Code game.
Languages available:
1. Using classes to make your code more efficient
Now that you’ve seen how classes work, you can write a class in your SpawnerSampler script. You can use classes to write more efficient code and group together similar functions. This is a key part of writing code for games: finding and using patterns to create the functionality and player experience that you want.
Let’s start by using what you already know to write a class to calculate the angle required for each health potion.
2. Write a class declaration
First, add a class declaration. Place this after the closing curly brace of the SpawnerSample class in the SpawnerSample script file:
You’ll find out more about the public keyword and its function in the next tutorial.
3. Identify the data type
Now you need to identify the type of data necessary to handle the work you want this function to control. To help you do this, think about the purpose of the class you want to create.
This class needs to compute angles for potion spawning, in a regular sequence. To do this, it will need to be able to store:
- The angle of the current potion (n)
- The number of degrees that need to be added to the position each time a new potion is spawned (x°)
This will enable it to calculate the following equation:
Position of new potion = n + x°
4. Declare variables for the necessary data
In your new class LootAngle, declare two integer variables to store the necessary data for the equation:
1. The angle of the current potion (call this angle)
2. The number of degrees to add for the next potion (call this step)
5. Check your variable declarations
Your new LootAngle class should now match the following code:
Remember, each instruction should end with a semicolon, so the compiler knows it is an individual instruction.
6. Add a function
Finally, you need to add a function which computes the next angle to use.
Add the following function to your LootAngle class:
7. Review the function
As you’ve done throughout this project, use what you already know to identify the key information in this function and try to work out what each part will do in your script.
Let’s break down the function:
1.
The function type here is int and not void. This is because the function is not only going to do something, it’s also going to return a result (the angle for the new potion).
Here, your code tells the compiler that the function NextAngle will return an integer (int) value. Previously you were using void (meaning “nothing”), because your functions were computing things but not returning any results.
2.
This instruction stores the current angle in a new variable called currentAngle.
3.
The next instruction computes the angle for the next potion and stores it back in the variable angle. The WrapAngle function is a function that we’ve written for you, which enables you to store an angle between 0° and 360°.
4.
This instruction returns the original angle (before it was advanced in the sequence) to the caller. The value to the right of the return keyword is what will be returned by the function.
8. Review the function’s computation
Let’s take a moment to check that the function completes your required equation:
This function should compute the position of each new potion, by adding your selected increment (step) to your current angle. Let’s start with angle = 0 and step = 45.
The first time you call the function, it will:
1. Store 0 in currentAngle
2. Change angle to 0 + 45 = 45
3. Return 0
The second time the function is called, it will:
1. Store 45 in currentAngle
2. Change angle to 45 + 45 = 90
3. Return 45
It will complete the same process every time it is called.
9. Initialize the angle and step variables
You’re nearly done! Now the only missing thing is initializing the angle and step variables.
You just told the computer that the LootAngle class should reserve space for those two values in memory, but you didn’t assign a value to them. By default, the value will be set to 0.
You could write a function called Init to initialize those values to parameters you give them. But classes have a very useful feature called the constructor.
Let’s explore what it does:
1. In the SpawnerSample script, create a new line at the top of the Start function (above the first call to SpawnPotion).
2. Add the following instruction to the line:
Remember when you learned how to create a new variable of a particular class type? The typename was followed by two parenthesis like a function. That’s because it actually is a function: the constructor.
10. Create parameters for the constructor
The constructor is a special function that has exactly the same name as your class and no type (for example, void) written before it. The compiler will create a “hidden” constructor by default which does nothing. If you create your own, you can give it specific parameters.
To create your own constructor:
1. In the SpawnerSample script, find the LootAngle class.
2. Beneath the instructions initializing the variables angle and step, add the following code:
3. Check your class against the following completed version:
Now the constructor takes one parameter: the increase each time the NextAngle function is called. This is stored in the step variable, and the angle variable is initialized to 0.
4. Return to the Start function in your SpawnerSample script and revise the LootAngle variable creation instruction to the following:
That’s it — you’ve written your first class and created a variable with it!
11. Update the Start function
Now that you’ve written your own class, you need to update the script to use it.
Adjust your script to use the LootAngle spawner class:
1. In the SpawnerSample script, find the Start function.
2. Replace the instructions inside the Start function with the following:
3. Save your changes to the script.
12. Test your changes
As always, it’s important to test changes in the Editor:
1. Return to Unity Editor and look at the Console window.
2. You should see five instances of error CS0122 displayed:

This error means that a function is inaccessible due to its protection level.
LootAngle.NextAngle() is the function you wrote. LootAngle.LootAngle is a function called LootAngle in the class LootAngle — it’s your constructor.
But why are they inaccessible? In the next tutorial, you’ll learn about the public keyword and how to make these functions accessible.
13. Tutorial summary
In this tutorial, you have:
- Written a custom class for your SpawnerSample script
- Used the constructor to initialize parameters
- Tested your changes in Unity Editor and identified a protection level error
In the next tutorial, you’ll fix the protection level error and learn about the public keyword. You’ll also begin to explore the concept of inheritance, and how this can be used to write more efficient code.