Skip to content

CS0029 Error: Cannot Implicitly Convert [LuaObject] Class to LuaValue in Unity Despite Following Documentation #212

@RanWIfe

Description

@RanWIfe

Hello everyone,
I'm using Unity version 6000.0.15f1 and Lua-CSharp version v0.4.2 (installed via NuGetForUnity and the Lua.Unity package from git URL as per the README).
I have a simple class marked with the [LuaObject] attribute, following the example in the documentation (e.g., the Vector3 wrapper):

using UnityEngine;
using Lua;

[LuaObject]
public partial class LuaVector3
{
    public Vector3 vector; 

    [LuaMember("x")]
    public float x
    {
        get => vector.x;
        set => vector = new Vector3(value, vector.y, vector.z);
    }

    [LuaMember("y")]
    public float y
    {
        get => vector.y;
        set => vector = new Vector3(vector.x, value, vector.z);
    }

    [LuaMember("z")]
    public float z
    {
        get => vector.z;
        set => vector = new Vector3(vector.x, vector.y, value);
    }

    [LuaMember("create")]
    public static LuaVector3 Create(float x, float y, float z)
    {
        return new LuaVector3()
        {
            vector = new Vector3(x, y, z)
        };
    }

    [LuaMember("normalized")]
    public LuaVector3 Normalized()
    {
        return new LuaVector3 { vector = Vector3.Normalize(vector) };
    }

    public Vector3 ConvertVector3()
    {
        return vector;
    }
}

However, when I try to assign an instance to the Lua environment like this:

state.Environment["Vector3"] = new LuaVector3();

I get the following compile-time error:
error CS0029: Cannot implicitly convert type 'LuaVector3' to 'Lua.LuaValue'
According to the README, the source generator should automatically create an implicit conversion to LuaValue for classes with [LuaObject]. I've followed the installation steps exactly:

Installed LuaCSharp via NuGetForUnity.
Added the Lua.Unity package via Package Manager with the git URL.
Cleaned the Library folder, restarted Unity, and rebuilt multiple times.

I've tried the following to troubleshoot, but nothing works:

Removing the ConvertVector3() method.
Commenting out all members and methods in the class (leaving it empty except for the attribute).
Replacing properties with simple int values (e.g., [LuaMember("test")] public int Test = 42;).

I genuinely don't understand what's wrong— it seems like the source generator isn't triggering or generating the implicit operator. There are no errors or warnings in the Unity console about the generator.
Has anyone encountered this? Any ideas on how to fix it or debug the source generator in Unity?
Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions