Write your own spawn function
Tutorial
·
Beginner
·
+10XP
·
30 mins
·
(140)
Unity Technologies

In this tutorial, you’ll:
- Write a function to spawn health potions in a more efficient way
- Check your code in Unity Editor
- Learn about scope, and how to access information in different scripts
When you’ve done this, you’ll be ready to think about organizing scripts at a higher level.
1. Write a function declaration
Functions enable programmers to write more efficient code, instead of copying multiple instructions. Now that you’ve seen how they work, you can create a new function to spawn a health potion.
Let’s start with a challenge — try to write a function declaration for the new function in the SpawnerSample script.
Use the following guidance to help you:
- Declare your function after the Start function, before the final curly brace in the script.
- The function type should be ‘void’, just like the example you reviewed.
- Select a name which gives some information about what the function will do.
- If there is a value that needs to change for different health potion spawns, then your function will need a parameter.
TIP: If you’re struggling, review the example declaration again and use it as a reference to help you.
When you’ve had a go at writing the function declaration, continue to the next step to see the solution.
2. Check your function declaration
Your function declaration should look like this:
void SpawnPotion(int angle)Let’s break this down:
- SpawnPotion is a descriptive function name, which is helpful for anyone who reads your code later. You may have chosen a different name — any clear and descriptive alternative will work just as well.
- The parameter variable type is int.
- The parameter variable in the example is called angle, because this is the only thing which changes for each spawned potion. Again, you may have chosen a different name in your own version.
The declaration is currently underlined in red in your code editor, because you haven’t added the function body in yet. You’ll fix this in the next step.
3. Write the function body
Now that you’ve written the function declaration, let’s write the function body:
1. On the line beneath the function declaration, open a set of curly braces ({).
2. On the next line, copy in the three instructions in your script which control the health potion spawn.
3. On a new line, close the curly braces (}).
4. Check your code against the example below:
void SpawnPotion(int angle)
{
Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
spawnPosition = transform.position + direction * radius;
Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
}Remember, your function and variable names may be different, but everything else should match the example.
Vector3 is a variable type which stores a position in the 3D gamespace as three values (the x-axis, y-axis and z-axis). These values are usually referred to in shorthand as x, y, and z. You don’t need to worry about the rest of the unfamiliar content in this function right now.
5. Press Ctrl + S (Windows) or Cmd + S (macOS) to save your changes.
Your code editor will have underlined spawnPosition and radius in red, to show you that there are errors in this code (like a word processing program). Don’t worry — you’ll identify and fix these errors in the next steps.
4. Identify the errors in your function
Before you fix the code, let’s find out more information about the errors:
1. Return to Unity Editor.
2. In the Project window, select the Console tab.

3. You should see the following error messages:
- error CS0103: The name 'spawnPosition' does not exist in the current context (this error will be duplicated twice)
- error CS0103: The name 'radius' does not exist in the current context

The Console is telling you that the spawnPosition and radius variables do not exist in “the current context” — let’s explore exactly what that means.
5. Introduction to scope
The curly braces in your script ({ }) define something called scope. This means that only variables declared inside the function exist there; imagine that the braces create separate little universes of instructions.
This means that in one script you could have two variables with the same name but assigned different values, existing independently in separate scopes.

In your script, the variables spawnPosition and radius are both declared in the Start function. This means that when the computer starts compiling the SpawnPotion function, it has no idea that they exist.
To fix this issue, you need to re-declare the variable by adding its type the first time you call it in the function. This tells the computer how much memory it needs to save for the variable’s information.
6. Re-declare the variables in your function
Let’s fix error CS0103, so your SpawnPotion works properly:
1. Return to your code editor, and find the SpawnPotion function in your SpawnerSample script.
2. Redeclare the radius variable within the scope of the new function. It’s simplest to do this before the instructions which calculate the spawning placement.
3. Add the Vector3 type in front of the spawnPosition variable.
4. Check your own code against the example below:
void SpawnPotion(int angle)
{
int radius = 5;
Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
Vector3 spawnPosition = transform.position + direction * radius;
Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
}
5. Press Ctrl + S (Windows) or Cmd + S (macOS) to save your changes.
7. Replace the spawning instructions with function calls
Now that you’ve written a SpawnPotion function, you can call (use) it to replace the sets of instructions in your Start function:
1. In the Start function of your SpawnerSample script, replace the set of instructions to spawn each health potion with a function call.
Use the following information to help you:
- You need four health potions to spawn at game start.
- Each potion is created at a different angle — you already have the information for this in the existing sets of instructions.
- Each instruction needs to end with a semicolon (;).
TIP: If you need a refresher, you can review the step on function calls in the previous tutorial.
2. Press Ctrl + S (Windows) or Cmd + S (macOS) to save your changes.
8. Check your function calls
Your SpawnerSample script should now have the following functions:
void Start()
{
SpawnPotion(0);
SpawnPotion(45);
SpawnPotion(90);
SpawnPotion(135);
}
void SpawnPotion(int angle)
{
int radius = 5;
Vector3 direction = Quaternion.Euler(0, angle, 0) * Vector3.right;
Vector3 spawnPosition = transform.position + direction * radius;
Instantiate(ObjectToSpawn, spawnPosition, Quaternion.identity);
}
NOTE: Remember, the function and variable names will be whatever you defined yourself.
Each function call in the Start function is assigned a different angle, so that the health potions are distributed correctly around the spawn point. If you want to add a fifth health potion, you just need to write an additional function call with a different angle.
9. Test your changes
Now you know that you’ve corrected your code, check your changes in the Unity Editor:
1. In the Editor, check the Console tab again. The CS0103 error messages should no longer be displayed.
2. Click Play to enter Play Mode.
3. Check that four (or five!) health potions now spawn at different angles when the game starts.
4. When you’ve finished checking your changes, click Play again to exit Play Mode.
10. Tutorial summary
In this tutorial, you have:
- Written a function to spawn health potions more efficiently
- Checked your function in the Console
- Learnt about scope in relation to variables
- Redeclared variables to fix scope errors
- Replaced the spawning instructions with your function call
In each tutorial, you’re learning and applying fundamental programming concepts to improve the SpawnerSample script. These changes may seem small, but your progress will enable you to have a big impact on the player experience for the Creator Kit: Beginner Code game.