Hey guys, the following code gives me a StackOverFlowException and I'm just wondering how i can fix it. Thanks :)

To be more specific, the weight section, highlighted in bold.

public class ItemBaseStats : ItemBase {

private int weight;
private int value;


public int Weight
{
    get { return Weight; }
  **set { Weight = value; }**

}

public int Value
{
    get { return Value; }
    set { Value = value; }
}

}

You have integer fields weight and value with lower case letters that you are not using at all and then you have Weight and Value with capital letters that are the names of your properties.

Your problem is that both your properties access themselves. You should be getting an endless recursion and an overflow exception in both the getter and the setter of both your properties.

public int Weight {
// getting the value of Weight tries to read from Weight which
// will try to read it from Weight which will try to read it...
   get { return Weight; } 
 // setting the value of Weight tries to assign to Weight which
// will try to assign to Weight which will try to assign it...
   set { Weight = value; }
 }

You have to either use the 2 int fields to “back up” the data that these properties read from and assign to

public int Weight
 {
     get { return weight; }
     set { weight = value; }
 }
 public int Value
 {
     get { return value; }
// can't test/not sure this will work. You might need to rename 'int value'
     set { this.value = value; }
 }

Or just use automatically backed up properties

public int Weight
 {
     get;
     set;
 }
 public int Value
 {
     get;
     private set; // getter and setter can even have different access modifiers
 }

It shouldn’t give you that error, at least not just that code. I’m not positive but I think a stack overflow is basically when a script has too much recursion or infinite recursion. You should check any scripts that set the weight and see if they are setting it multiple times or if something recalls the set script. In mono develop you should be able to right click Weight and click references. That will show you every script that calls it.