keijiro / KlakSpout

Spout plugin for Unity
The Unlicense
651 stars 97 forks source link

Fix for : NullReferenceException: Object reference not set to an instance of an object at Klak.Spout.Blitter.GetMaterial #76

Closed clementshimizu closed 2 years ago

clementshimizu commented 2 years ago
NullReferenceException: Object reference not set to an instance of an object
  at Klak.Spout.Blitter.GetMaterial (Klak.Spout.SpoutResources resrc) [0x0000d] in <7fe4b2887b084643bde251e32ad9b866>:0 
  at Klak.Spout.Blitter.Blit (Klak.Spout.SpoutResources resrc, UnityEngine.Texture src, UnityEngine.RenderTexture dst, System.Boolean alpha) [0x00000] in <7fe4b2887b084643bde251e32ad9b866>:0 
  at Klak.Spout.SpoutSender.Update () [0x00081] in <7fe4b2887b084643bde251e32ad9b866>:0 

Klack Spout will not have access to SpoutResources if you add the spoutsender component from a script at runtime.

If I add spoutsender to a camera via scripting, it works fine in edit mode but fails in the build. The issue is that SpoutResources is connected via an editor script and saved in the editor folder which isn't accessible at runtime. It is stripped at compile-time.

I have this issue addressed in my project by the script function "SpoutSender.SetResource(...).

However, I suggest a different way of connecting the "blit shader", possibly loading from a "Resource" folder instead of the editor folder at startup, so the step I have to take is done automatically.

Thank you and I love your plugins.

keijiro commented 2 years ago

First of all, it's documented on README.md.

However, I suggest a different way of connecting the "blit shader", possibly loading from a "Resource" folder instead of the editor folder at startup, so the step I have to take is done automatically.

Use of the Resources folder is not recommended in general. At least, I should use Addressables instead of it. But I think most users are not familiar with Addressables, so I chose to use a "dumb way" to manage the resources.

keijiro commented 2 years ago

I'm closing this issue now. Please feel free to reopen it for further problems.

clementshimizu commented 2 years ago

Thank you! That helps

Fenikkel commented 2 years ago

In my case, I had this error creating senders in texture mode via code, but it was fixed (in editor and also in build) adding a spoutsender to a GameObject and sending the Game View or a simple texture.

I'm using videos that are in the streaming assets folder.

AlexksGud commented 2 years ago

NullReferenceException: Object reference not set to an instance of an object Klak.Spout.Blitter.GetMaterial (Klak.Spout.SpoutResources resrc) (at Library/PackageCache/jp.keijiro.klak.spout@2.0.3/Runtime/Internal/Utility.cs:45) Klak.Spout.Blitter.Blit (Klak.Spout.SpoutResources resrc, UnityEngine.Texture src, UnityEngine.RenderTexture dst, System.Boolean alpha) (at Library/PackageCache/jp.keijiro.klak.spout@2.0.3/Runtime/Internal/Utility.cs:25) Klak.Spout.SpoutSender.Update () (at Library/PackageCache/jp.keijiro.klak.spout@2.0.3/Runtime/SpoutSender.cs:112)

Having error in runtime and in game mode when I adding Spot Sender via scripting It works only in game mode when I add SpoutSender by my hands

Here is the script, I guess problem is on SpoutResorces Exmp

keijiro commented 2 years ago

@AlexksGud You should set a valid instance of SpoutResources, not an empty one.

AlexksGud commented 2 years ago

@keijiro I know " but you must give the SpoutResources asset (which holds references to the package assets) after instantiation." -i didnt understand this in README Anyway , I renamed Resources folder in Assets folder , and all works . But there must be a better way to solve it

keijiro commented 2 years ago

SpoutResources is just a subclass of ScriptableObject. I'd recommend checking ScriptableObject tutorials for details.