Unable to play scripts through C# script - System.NullReferenceException

The engine is not working as expected? Documentation is wrong or out of date? Let us know here!
Forum rules
We are not providing support via this forum; it's community-driven. For official support see https://naninovel.com/support/#naninovel-support
Post Reply
Devroom-Vantasia
Posts: 2
Joined: 26 Mar 2025 06:32

Unable to play scripts through C# script - System.NullReferenceException

Post by Devroom-Vantasia »

I'm trying to call a nani script to start playing through a C# script, but it gives me back an error for "System.NullReferenceException: Object reference not set to an instance of an object" each time.

I used the following code based off of the engine's own PlayScript.cs. PlayScript.cs works as intended, but it doesn't work in my own code:

Code: Select all

public void Enter()
{
	var scriptPlayer = Engine.GetServiceOrErr<IScriptPlayer>();
	scriptPlayer.LoadAndPlay(enterScript).Forget();
}

Even when I try to play it through a PlayScript component directly, it throws the same error:

Code: Select all

public void Enter()
{
    playScript.scriptPath = enterScript;
    playScript.Play();
}

This is the error message I receive:

Code: Select all

System.NullReferenceException: Object reference not set to an instance of an object
  at Naninovel.ScriptPlaylist.GetNestedHostIndex (System.Int32 nestedIndex, System.Nullable`1[T] hostIndent) [0x00008] in .\Packages\com.elringus.naninovel\Runtime\ScriptPlayer\ScriptPlaylist.cs:178 
  at Naninovel.ScriptPlaylist.GetNestedHost (System.Int32 nestedIndex, System.Nullable`1[T] hostIndent) [0x00000] in .\Packages\com.elringus.naninovel\Runtime\ScriptPlayer\ScriptPlaylist.cs:194 
  at Naninovel.ScriptPlayer.SelectNextCommand () [0x00103] in .\Packages\com.elringus.naninovel\Runtime\ScriptPlayer\ScriptPlayer.cs:631 
  at Naninovel.ScriptPlayer.PlayRoutine (Naninovel.AsyncToken token) [0x002e4] in .\Packages\com.elringus.naninovel\Runtime\ScriptPlayer\ScriptPlayer.cs:561 
  at Naninovel.UniTaskCompletionSource.Naninovel.Async.IAwaiter.GetResult () [0x00013] in .\Packages\com.elringus.naninovel\Runtime\Common\Async\UniTask\Public\UniTaskCompletionSource.cs:89 
  at Naninovel.UniTask.GetResult () [0x00000] in .\Packages\com.elringus.naninovel\Runtime\Common\Async\UniTask\Public\UniTask.cs:44 
  at Naninovel.UniTask+Awaiter.GetResult () [0x00000] in .\Packages\com.elringus.naninovel\Runtime\Common\Async\UniTask\Public\UniTask.cs:199 
  at Naninovel.UniTaskExtensions.ForgetCore (Naninovel.UniTask task) [0x00016] in .\Packages\com.elringus.naninovel\Runtime\Common\Async\UniTask\Public\UniTaskExtensions.cs:187 
UnityEngine.Debug:LogError (object)
Naninovel.Async.UniTaskScheduler:PublishUnobservedTaskException (System.Exception) (at ./Packages/com.elringus.naninovel/Runtime/Common/Async/UniTask/UniTaskScheduler.cs:55)
Naninovel.Async.CompilerServices.AsyncUniTaskVoidMethodBuilder:SetException (System.Exception) (at ./Packages/com.elringus.naninovel/Runtime/Common/Async/UniTask/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs:27)
Naninovel.UniTaskExtensions/<ForgetCore>d__11:MoveNext () (at ./Packages/com.elringus.naninovel/Runtime/Common/Async/UniTask/Public/UniTaskExtensions.cs:187)
Naninovel.Async.CompilerServices.AsyncUniTaskVoidMethodBuilder:Start<Naninovel.UniTaskExtensions/<ForgetCore>d__11> (Naninovel.UniTaskExtensions/<ForgetCore>d__11&) (at ./Packages/com.elringus.naninovel/Runtime/Common/Async/UniTask/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs:75)
Naninovel.UniTaskExtensions:ForgetCore (Naninovel.UniTask)
Naninovel.UniTaskExtensions:Forget (Naninovel.UniTask) (at ./Packages/com.elringus.naninovel/Runtime/Common/Async/UniTask/Public/UniTaskExtensions.cs:169)
Naninovel.ScriptPlayer:Resume (System.Nullable`1<int>) (at ./Packages/com.elringus.naninovel/Runtime/ScriptPlayer/ScriptPlayer.cs:295)
Naninovel.ScriptPlayer:Play (string,int) (at ./Packages/com.elringus.naninovel/Runtime/ScriptPlayer/ScriptPlayer.cs:278)
Naninovel.ScriptPlayerExtensions/<LoadAndPlay>d__0:MoveNext () (at ./Packages/com.elringus.naninovel/Runtime/ScriptPlayer/ScriptPlayerExtensions.cs:17)
Naninovel.Async.CompilerServices.AsyncUniTaskMethodBuilder:Start<Naninovel.ScriptPlayerExtensions/<LoadAndPlay>d__0> (Naninovel.ScriptPlayerExtensions/<LoadAndPlay>d__0&) (at ./Packages/com.elringus.naninovel/Runtime/Common/Async/UniTask/CompilerServices/AsyncUniTaskMethodBuilder.cs:103)
Naninovel.ScriptPlayerExtensions:LoadAndPlay (Naninovel.IScriptPlayer,string,int)
MapSegment:Enter () (at Assets/Scripts/MapSegment.cs:112)
OverworldMovement:EnterLocation () (at Assets/Scripts/OverworldMovement.cs:122)
UnityEngine.EventSystems.EventSystem:Update () (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/EventSystem/EventSystem.cs:530)

If anyone knows what could be causing this or how to resolve it, then I'd really appreciate the help.

Thank you

Devroom-Vantasia
Posts: 2
Joined: 26 Mar 2025 06:32

Re: Unable to play scripts through C# script - System.NullReferenceException

Post by Devroom-Vantasia »

Update: I fixed it. My error was that I was using a custom command as the first line of a script, which causes the error above. Make it so that the first line of a script isn't a custom command (it can be a build-in command), and it seems to stop happening.

Post Reply