webgl LuaException: attempt to call a nil value (method 'AddForce')
unity 2022.3.11f
I'm using a slightly modified lua code (02_U3DScripting) as an example.
local speed = 10
local lightCpnt = nil
local rb
function start()
print("lua start...")
print("injected object", lightObject)
lightCpnt= lightObject:GetComponent(typeof(CS.UnityEngine.Light))
rb = self:GetComponent(typeof(CS.UnityEngine.Rigidbody))
rb:AddForce(CS.UnityEngine.Vector3(0, 1000, 0))
local rnd = CS.UnityEngine.Random.Range(0.0, 1000.0)
print(rnd, self.transform.position)
end
function update()
local r = CS.UnityEngine.Vector3.up * CS.UnityEngine.Time.deltaTime * speed
self.transform:Rotate(r)
lightCpnt.color = CS.UnityEngine.Color(CS.UnityEngine.Mathf.Sin(CS.UnityEngine.Time.time) / 2 + 0.5, 0, 0, 1)
end
function ondestroy()
print("lua destroy")
end
Only when building webgl, the following errors occur:
LuaException: [string "LuaTestScript"]:18: attempt to call a nil value (method 'AddForce') stack traceback: [string "LuaTestScript"]:18: in function <[string "LuaTestScript"]:11> at XLua.LuaEnv.ThrowExceptionFromError (System.Int32 oldTop) [0x00000] in <00000000000000000000000000000000>:0
LuaException: [string "LuaTestScript"]:18: No such type: UnityEngine.Random.Range stack traceback: [C]: in function 'error' [string "Init"]:42: in field 'Range' [string "LuaTestScript"]:18: in function <[string "LuaTestScript"]:11> at XLua.LuaEnv.ThrowExceptionFromError (System.Int32 oldTop) [0x00000] in <00000000000000000000000000000000>:0