Exceptions when using kopilua in unity3d

i got a problem when i using the kopilua in unity.
I add this code in the update function :L.GetFunction(“test.foo”).Call(), and the test.foo function is define in the test.lua;
After some times, kopilua throw some exceptions;

Here are the source code

using UnityEngine;
 using System.Collections;
 using LuaInterface; 
 public class KLITest : MonoBehaviour
	 Lua L = new Lua(); 
	 void Start() { L.DoFile(Application.dataPath + "/test.lua"); } 
	 void Update() { L.GetFunction("test.foo").Call(); }


test = {}

function test.foo()



the excptions are not always the same;
1.LuaException: unprotected error in call to Lua API (attempt to index a nil value)

2.NullReferenceException: Object reference not set to an instance of an object KLITest.Update () (at Assets/KLITest.cs:15)

3.InvalidCastException: Cannot cast from source type to destination type.
	LuaInterface.Lua.GetFunction (System.String fullPath) (at C:/Users/gfoot/Documents/Lua/KLI/KopiLuaInterface/src/Lua.cs:347)
	KLITest.Update () (at Assets/KLITest.cs:15)

and something else;
so i want to know what causing the exceptions.

Try this Lua code:

test = {}
function test.foo()

This line was probably throwing the exception:


You need to load the UnityEngine assembly before you can access its contents in Lua. [Correction: I just tested, and you don’t need to load the assembly.]

Let me know if this works. Otherwise please confirm which line in the Lua script is throwing the exception, and whether it’s happening in your behaviour’s Start() or Update() method.

Sorry I’m a bit late coming to this - this bug is due to some errors in the way LuaInterface uses finalizers in a few classes, including the LuaFunction class. I fixed this in KopiLuaInterface in late September, and made a release here:

Upstream LuaInterface has a different fix, but I prefer mine.