Lesson 6.2 - Research and Troubleshooting
Tutorial
·
Beginner
·
+10XP
·
30 mins
·
(418)
Unity Technologies

Overview:
Adding new features to a game often involves research and problem-solving. In this lesson, we’ll simplify the process by adding a new feature to the Prototype 1 game. You’ll learn how to conduct online research, troubleshoot bugs, and overcome unexpected challenges—skills you can apply to your own projects.
Project Outcome:
By the end of this lesson, you will be able to switch between two cameras in the scene by pressing a specified key.
1. Researching possible solutions
You want to add a feature to our Prototype 1 game that allows players to switch between two cameras by pressing the C key.
Use a search engine to find answers.
When searching through a search engine, you’ll often find valuable discussions on Unity Discussions. Below is an example of a search and the results provided by the search engine.

These are great resources because they showcase how others have tackled and solved similar challenges. However, we also have another powerful tool at our disposal: AI solutions like ChatGPT or Unity Muse.
Let’s look at the results we get when using these tools.
2. Use an AI tool
To complete this step, you’ll need to use an AI tool of your choice. We recommend Unity Muse for Unity-specific tasks, but you can also use other AI-powered tools like ChatGPT (by OpenAI), Google Bard, or Anthropic Claude.
1. Use the prompt below in your AI tool:
I have two cameras in my Unity game, I want the game to switch between the two cameras when the player presses the C button. How can I do that?
In our case, this was the reply we received:
To switch between two cameras when the player presses the C button in Unity, you can use a script to toggle their enabled state. Here’s how you can do it:
Step 1: Set Up Your Cameras
- Make sure both cameras exist in your scene and are properly positioned.
- Assign meaningful names, like Camera1 and Camera2.
- Disable one of the cameras in the Inspector (e.g., Camera2) so that only one is active at the start.
Step 2: Create the Camera Switching Script
Create a new script called CameraSwitcher and attach it to any GameObject in the scene (e.g., an empty GameObject named CameraManager).
using UnityEngine;
public class CameraSwitcher : MonoBehaviour
{
public Camera camera1; // First camera
public Camera camera2; // Second camera
private bool isCamera1Active = true; // Tracks which camera is active
void Start()
{
// Ensure one camera is enabled, and the other is disabled at the start
camera1.enabled = true;
camera2.enabled = false;
}
void Update()
{
// Check if the C key is pressed
if (Input.GetKeyDown(KeyCode.C))
{
SwitchCameras();
}
}
void SwitchCameras()
{
// Toggle cameras
isCamera1Active = !isCamera1Active;
camera1.enabled = isCamera1Active;
camera2.enabled = !isCamera1Active;
}
}
2. Follow the steps above or the steps from your AI tool to add the new function to your game.
3. Troubleshooting
If you followed the instructions from our responses, you will notice that the switch is not working and that an error is generated in the console. This is a common problem that can arise, and knowing how to troubleshoot is a critical skill.
The error we received in the console was:
“UnassignedReferenceException: The variable camera1 of CameraSwitcher has not been assigned.”
We went back to our AI tool and replied with this prompt:
The script is not working and I get this error when running the game: UnassignedReferenceException: The variable camera1 of CameraSwitcher has not been assigned.
What is wrong and how can I fix it?
This is the response we received:
The error "UnassignedReferenceException: The variable camera1 of CameraSwitcher has not been assigned" occurs when the camera1 or camera2 variables in your script have not been assigned in the Unity Inspector.
Here’s how you can fix this:
Step 1: Check Script Attachment
Make sure the CameraSwitcher script is attached to a GameObject in your scene. If it’s not attached, Unity cannot execute it.
- Select an existing GameObject (or create a new empty GameObject).
- Drag and drop the CameraSwitcher script onto the GameObject.
Step 2: Assign Cameras in the Inspector
- Select the GameObject with the CameraSwitcher script.
- In the Inspector panel:
- Look for the CameraSwitcher script component.
- You will see two public fields: Camera1 and Camera2.
- Drag and drop your cameras from the Hierarchy into these fields:
- Drag the Main Camera (or whichever is Camera1) into the camera1 slot.
- Drag the second camera (e.g., SecondaryCamera) into the camera2 slot.
Step 3: Verify Cameras Exist in Your Scene
- Make sure both cameras exist in your scene Hierarchy and are enabled.
- If you accidentally deleted one, recreate it via GameObject > Camera.
Why This Happens:
The UnassignedReferenceException occurs because public variables like camera1 and camera2 are exposed in the Inspector. Unity requires you to manually assign references for these fields in the Editor; otherwise, they remain null, causing the error.
Looking at the response and my current setup in the scene, it is clear that step 2 addresses the problem, since we forgot to assign the cameras in the Inspector.
To fix it, we select the CameraManager GameObject in the Hierarchy. In the Inspector, we assign the cameras to the respective slots in the script component.
If we run the game now, the cameras should switch when you press the C key.
4. Final Thoughts
AI tools are excellent for this type of research because they allow you to ask questions using natural language and provide step-by-step solutions tailored to your specific needs. This significantly speeds up the research process, as you no longer need to sift through countless forum threads or documentation to find a solution.
However, be careful when relying on AI tools. Current AI models can sometimes "hallucinate", meaning they may generate incorrect or misleading responses. If what you're trying doesn't work, give feedback to the AI tool about what went wrong, make your prompt more specific, or try a different AI Gen tool.