
How to Make NPCs Talk with ChatGPT in Unity
"ChatGPT NPC" is how most Unity developers first picture an AI character: send what the player typed to a language model, show the answer over the NPC's head. This guide builds exactly that with the OpenAI API, then shows what it leaves you to solve and how the same NPC looks with the Neocortex Unity SDK.
Route 1: call the OpenAI API yourself
You need an OpenAI API key and a Unity project. The NPC keeps a list of messages, starting with a system prompt that says who the character is, and sends the whole list each time the player speaks:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
public class ChatGptNpc : MonoBehaviour
{
[Serializable] class Message { public string role; public string content; }
[Serializable] class ChatRequest { public string model; public Message[] messages; }
[Serializable] class Choice { public Message message; }
[Serializable] class ChatResult { public Choice[] choices; }
[SerializeField] private string apiKey; // for local testing only, never ship it
private readonly List<Message> history = new()
{
new Message { role = "system", content = "You are Brannoch, a gruff village blacksmith. Answer in one or two sentences." }
};
public IEnumerator Ask(string playerLine)
{
history.Add(new Message { role = "user", content = playerLine });
string body = JsonUtility.ToJson(new ChatRequest { model = "MODEL_NAME", messages = history.ToArray() });
using var request = new UnityWebRequest("https://api.openai.com/v1/chat/completions", "POST");
request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(body));
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer " + apiKey);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success) { Debug.LogWarning(request.error); yield break; }
Message reply = JsonUtility.FromJson<ChatResult>(request.downloadHandler.text).choices[0].message;
history.Add(reply);
Debug.Log(reply.content); // show it in your dialogue UI
}
}Replace MODEL_NAME with a current model from OpenAI's docs, call StartCoroutine(npc.Ask("Got any work?")), and Brannoch answers. That's a working prototype in about forty lines.
What the prototype leaves you to build
The first conversation is the easy part. Shipping it means solving:
- The key. An OpenAI key in a build can be pulled out by anyone and spends your whole account. You need a server of your own between the game and the API.
- Voice. Speech-to-text for the player, text-to-speech for the reply, and streaming between them, or every answer waits for both to finish. Our voice pipeline guide breaks it down.
- Emotion and actions. The reply is plain text. To drive animation or gameplay you prompt for JSON, parse it and handle the times it comes back malformed.
- History. The list grows with every line, and so do the cost and the delay. You need trimming or summaries.
- Staying in character. A system prompt alone breaks quickly when players push. See how to keep AI NPCs in character.
- Cost control. Nothing stops one player from talking all day on your bill.
Route 2: the same NPC with Neocortex
With Neocortex you design Brannoch once in a browser editor, with a personality, a world, a knowledge base, a voice and actions, and the Unity SDK handles the rest:
- In Unity, add the package from git URL:
https://github.com/neocortex-link/neocortex-unity-sdk.git - Paste your API key in Tools > Neocortex > Settings.
- Right-click the Hierarchy and choose Neocortex > Complete Voice Chat, then pick Brannoch in the Smart Agent's dropdown.
- Press Play and talk.
From code, a text question with a spoken answer is one call:
agent.TextToAudio("Got any work?"); // agent is a NeocortexSmartAgentEach reply arrives as lines with an emotion on each, plus any actions with the id of the object they target, so animation and gameplay hook into Unity events instead of parsed text. Our step-by-step Unity AI character tutorial shows the whole setup.
Side by side
| OpenAI API directly | Neocortex Unity SDK | |
|---|---|---|
| First text reply | About 40 lines of code | No code |
| Voice in and out | Build it: STT, TTS, streaming | One component, streamed |
| Emotion per line | Prompt for it and parse | Built in, 17 emotions |
| Actions | Prompt for JSON and validate | Defined in the editor, sent as data |
| Key protection | Your own server | Per-player and per-character limits on each key |
| WebGL microphone | Not in Unity | Included template |
| Character design | A system prompt | Visual editor with free testing |
Common questions on the do-it-yourself route
- "The reply is cut off." The model hit its output limit. Ask for short answers in the system prompt, which is also better dialogue.
- "It forgot what we said." You must send the whole history every time. A language model doesn't recall anything between requests.
- "Can I ship it as a WebGL build?" Not with the key inside: anyone can read it in the browser's network tab. Send requests through your own server.
Next steps
To go further with Neocortex in Unity, add voice chat and text-to-speech for scripted lines. If you'd rather run a model on your own machine while you prototype, see how to run an LLM locally in Unity. Every tutorial is in the guide to building AI NPCs.
Checked November 2026. Neocortex is not affiliated with OpenAI.
Frequently Asked Questions
Can I use ChatGPT for NPCs in Unity?
Yes. You can call the OpenAI API from Unity with UnityWebRequest and a system prompt that describes the character. For voice, emotions, actions and key limits without building them yourself, Neocortex gives you a full character through its Unity SDK.
Is it safe to put an OpenAI API key in a Unity game?
No. Anything in a build can be extracted, and an OpenAI key gives full access to your account's spending. If you call OpenAI directly, route requests through your own server. Neocortex keys can carry per-player and per-character usage limits that our servers enforce, and you can deactivate a key from the dashboard.
How do I keep a ChatGPT NPC in character?
Write a specific system prompt, describe the world, limit what the character knows and test it against players who try to break it. Neocortex characters are built in an editor with a world, a knowledge base and actions, and stay inside the world you describe.
How do I make a ChatGPT NPC speak?
You need speech-to-text for the player, text-to-speech for the reply, and streaming between them so the wait stays short. The Neocortex Unity SDK does all three from one component.
Written by
Sercan Altundas
Date
Nov 15, 2026
Read time
9 min






