How can I expose interface's variables?

I want to manipulate some class’s variables in unity inspector, which inherits from interface.

public interface MyInter {
    int number { get; set; }
}
[Serializable]
public class MyClass1 : MyInter{
     public int number {get; set;}
    public int someInt;
}
public class InterTest : MonoBehaviour , MyInter {
    public MyClass1 my1 = new MyClass1();
    public int number {get; set;}
}

so there is no ‘number’ variable at inspector.

How can this appear? can’t?

Thanks.

I guess because it is a property. Unity ignores properties right? Does it appear if it is a plain int without the get; set; ?

I agree with Korno, try just declaring them as public members without the get/set accessors.

interface can’t have field.

Whoops. My mistake, I totally overlooked the first few lines :stuck_out_tongue: Carry on.

Ah right. Try declaring it as an abstract class then. They can have fields or does it really need to be an interface?

Yes need to be interface.
There is cases some class already inherited some abstract class now also need to have some variables from interface.

Ok if the property has a private variable behind it you could force that to be serialised:

public class MyClass1 : MyInter{
  
[SerializeField]
protected int _number;
public int number {get {return _number;} set{_number=value;}}
    public int someInt;
}

That will force _number to be in the inspector while keeping it private in code. That way you kind of have access to number.

1 Like

Thx, but not much help.

So why use interface? Because for passing class instances as parameter.

public interface MyInter {
    int number { get; set; }
}
[Serializable]
public class MyClass1 : MyInter{
     public int number {get; set;}
}
[Serializable]
public class MyClass2 : MyInter{
     public int number {get; set;}
}
public class InterTest : MonoBehaviour{
    public MyInter[] inters ={new MyClass1(), new MyClass2()};
    void Start(){
        foreach(MyInter t in inters){
           t._number = ....
        }
    }
}

This result error because [t] does not have _number variable.
It is specific class’s variable. → Can’t use foreach → useless…

(of course even after use your code,
public int _number;
public int number {get {return _number;} set{_number=value;}}
)

You access the property rather than the field. That’s why you defined the property on the interface.

If you have an object cast to any interface, you can’t access anything on it except that which is on the interface. The compiler wouldn’t know anything else about it in the context of that code.

public interface INumberContainer
{

   int Number { get; set; }

}

public class ConcreateNumberContainerA : INumberContainer
{

   private int _number;
   
   public int Number
   {
     get { return _number; }
     set { _number = value; }
   }

}

public class ConcreateNumberContainerB : INumberContainer
{

   private int _number;
   
   public int Number
   {
     get { return _number; }
     set { _number = value; }
   }

}


public class UsageExample
{
  
   public INumberContainer[] containers; = { new ConcreateNumberContainerA(),
                        new ConcreateNumberContainerB() };

   void Start()
   {
     foreach(INumberContainer c in containers)
     {
       c.Number = 5;
     }
   }

}
[Serializable]
public class MyClass1 : MyInter{
[SerializeField]
private int _number;    
public int number {get {return _number;} set{_number = value;}}
}

[Serializable]
public class MyClass2 : MyInter{
[SerializeField]
private int _number;    
public int number {get {return _number;} set{_number = value;}}
}
public class InterTest : MonoBehaviour{
    public MyInter[] inters ={new MyClass1(), new MyClass2()};
    void Start(){
        foreach(MyInter t in inters){
// Look here!          
t.number = ....
        }
    }
}

Why wont that work? Dont access _number access number as thats is the interface definition (internally it is just going to return _number)

@lordofduct beat me to it :smile:

not work…

public class InterTest : MonoBehaviour {
    public bar barset = new bar();
    public bar2 barset2 = new bar2();
    public FirstInter[] tests = { new bar(), new bar2() };
   
    void Start() {
        foreach (FirstInter t in tests) {
            Debug.Log(t.number);
        }
    }
}

public interface FirstInter {
    void DoSome(int a, int b);
    int number { get; set; }
}

[Serializable]
public class bar : FirstInter {
    
    public void DoSome(int a, int b) {
        Debug.Log(a+b);
    }
    public int num;
    public int number { get { return num; } set { num = value; } }
}
[Serializable]
public class bar2 : FirstInter {
    public void DoSome(int a, int b) {
        Debug.Log((a - b)+" bar2 result ");
    }
    public int num;
    public int number { get { return num; } set { num = value; } } 
}

So even set 3, 5 at inspector’s barset, barset2’s num variable, Debug.Log(t.number) says 0, 0

so my mistake…

after I revised, it seems work.

public bar barset = new bar();
    public bar2 barset2 = new bar2();
//    public FirstInter[] tests = { barset, barset2 };
    public List<FirstInter> tests = new List<FirstInter>();
    void Start() {
        tests.Add(barset);
        tests.Add(barset2);
        foreach (FirstInter t in tests) {
            Debug.Log(t.number);
        }
    }

If still anyone have problem with setting value of the variables inherited from an interface in inspector, Thanks to Jetbrains Rider , I found a solution. just use [field: SerializeField] before introducing the variable in the child script.

example :

public interface IAlive
{
    float HealthPoint { get; set;}
}
public class Cat : MonoBehaviour , IAlive
{
    [field: SerializeField] float HealthPoint { get; set;}
}
2 Likes

Yes, these new directed attributes are a feature that was only recently supported in Unity and not available 6 years ago when this thread was active.

2 Likes