Laurent Crivello
Laurent Crivello

Reputation: 3931

How to Debug.Log a custom class

I have a custom class in C# for Unity, which contains multiple ints.

public class MyVector2Int
{

    public int x;
    public int y;

    public MyVector2Int(int xget, int yget)
    {
        x = xget;
        y = yget;
    }

    public static implicit operator string(MyVector2Int obj)
    {
        return "(" + obj.x + "," + obj.y + ")";
    }
}

However when I Debug.Log my class, I don't get desired result:

void MyFunction()
{
MyVector2Int v;
    v=new MyVector2Int(3,4);
    Debug.Log("Result:"+v);   // --> Expected Result:(3,4)
                              // --> Getting  Result:MyVector2Int
}

How can I update my class to display the expected result ?

Upvotes: 0

Views: 1083

Answers (1)

Xerillio
Xerillio

Reputation: 5261

You should be overwriting the ToString() method, not create an implicit type conversion:

public override string ToString()
{
    return "(" + obj.x + "," + obj.y + ")";
}

Upvotes: 4

Related Questions