Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by redphaze · Apr 15, 2017 at 03:20 AM · errorrpgstackoverflow

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; }
 }

 

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mayernikd · Apr 15, 2017 at 05:27 PM 0
Share

I am not sure why it is throwing a StackOverflowException. However, change the private variable that is declared as "value" for sure. That is definitely contributing to the problem. The contextual keyword value is used in the set accessor in ordinary property declarations -as you are using for Weight and Value. However, having an actual private property named value will cause the contextual keyword in the set accessor to behave with adverse side-effects. namely, it may cause the exception.

https://msdn.microsoft.com/en-us/library/a1khb4f8.aspx

Change the private properties to _value and _weight. That is a better convention to use for private, class-level properties in C# anyway.

[1]: https://msdn.microsoft.com/en-us/library/a1khb4f8.aspx

avatar image redphaze · Apr 16, 2017 at 02:13 AM 0
Share

Ah yeah thanks, i changed it to worth, thanks for pointing that out :)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by NoseKills · Apr 15, 2017 at 10:09 AM

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
  }
  
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image awinebrenner · Apr 15, 2017 at 04:16 PM 0
Share

How did I miss that at least I was right about what stack overflow was lol

avatar image redphaze · Apr 16, 2017 at 02:12 AM 0
Share

Hey thanks, all i had to do was change the weight and value to lower case and it worked :)

avatar image
0

Answer by awinebrenner · Apr 15, 2017 at 07:57 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

BurgZergArcade RPG - BaseCharacter script errors. 3 Answers

WHY SAYS ME THAT IS AN ERROR? 1 Answer

I'm getting a StackOverFlowException 1 Answer

Error CS1503, Error CS1502 1 Answer

Targetting Script Error Help 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges