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 /
This question was closed Jan 03, 2016 at 05:45 PM by ading25 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ading25 · Jan 03, 2016 at 09:06 AM · c#startpropertiesawakeinitialization

C# property keeps getting overwritten

I'm creating a game in which I want to have multiple types of units, so I have a BaseUnitClass.cs that the multiple types of units inherit from. BaseUnitClass.cs has a property called "Range" (for attack range) that each inheriting class will set depending on the type of unit.

The problem is that when I set the value and look in the debug log, the value seems to be set to what I want it to be set to, but immediately gets rewritten to 0 and I'm not sure why. I'm guessing it has something to do with the way I'm using the Awake/Start functions.

Does anyone have any clue why this might happen? I'm new to C# so I'm just guessing it's something subtle with the language that I don't know. Thanks!

 public class BaseUnitClass : MonoBehaviour {
     public float AttackRange { get; set; }
     void Start () {
         Debug.Log("AttackRange: " + AttackRange);
             transform.Find("Range").GetComponent<CircleCollider2D>().radius = AttackRange;
     }
 }
 
 public class SwordsmanUnitClass : BaseUnitClass {   
     void Awake() {
         AttackRange = 10f;
     }
 }


alt text

screen-shot-2016-01-03-at-120629-am.png (35.0 kB)
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 karl_jones ♦♦ · Jan 03, 2016 at 01:28 PM 1
Share

How many components do you have in the scene? Looks like you have 2 since you have 2 debug messages. Could it be that one is set and one is not?

avatar image ading25 karl_jones ♦♦ · Jan 03, 2016 at 04:48 PM 0
Share

No, that's the strange thing is that I just have the one component, but it seems to be set to 10 and then immediately changed to 0

2 Replies

  • Sort: 
avatar image
1

Answer by jmonasterio · Jan 03, 2016 at 04:52 PM

Put a breakpoint on line 5, and then when you hit the breakpoint (apparently twice), take a look at the call stack each time to what is calling the code the second time.

Comment
Add comment · Show 6 · 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 hexagonius · Jan 03, 2016 at 05:01 PM 2
Share

additional guess: you accidentally added the BaseUnitClass script to the same or another object in the same scene.

avatar image ading25 · Jan 03, 2016 at 05:06 PM 0
Share

Thanks, I tried that and both calls to the Start function were made by BaseUnitClass. I also saw what I expected from the debug log which is that in the first Start call, AttackRange = 10, but in the second call AttackRange = 0. I only have one BaseUnitClass, and @hexagonius I only have one BaseUnitClass script attached.

avatar image jmonasterio ading25 · Jan 03, 2016 at 05:14 PM 0
Share

When you look at the SwordsmanUnitClass component in the Inspector, what is the value on the AttackRange property (in the editor before you RUN)?

Temporarily, I would combine both SwordsmanUnit class and BaseUnit class into one class (with no inheritance) to see if you have the same issue. That way you can eli$$anonymous$$ate any "language issue" with C#, although I don't think it's that.

avatar image hexagonius ading25 · Jan 03, 2016 at 05:19 PM 1
Share

do you have both classes in the same script file? If so separate them.

avatar image jmonasterio hexagonius · Jan 03, 2016 at 05:44 PM 0
Share

I think it is possible to have more than one class in a file. Perhaps not common, but it certainly doesn't affect anything.

Show more comments
avatar image
1

Answer by ading25 · Jan 04, 2016 at 01:23 PM

Thanks everyone for helping! I figured out a way to resolve the issue.

Instead of trying to set the SwordsmanUnitClass values in the Awake() method and then using those methods in the BaseUnitClass's Start() method, I instead decided to override the BaseUnitClass's Start() method in the SwordsmanUnitClass and call it from that overrided method instead so I have something like this:

     public class BaseUnitClass : MonoBehaviour {
         public float AttackRange { get; set; }
         protected virtual void Start () {
             Debug.Log("AttackRange: " + AttackRange);
                 transform.Find("Range").GetComponent<CircleCollider2D>().radius = AttackRange;
         }
     }
     
     public class SwordsmanUnitClass : BaseUnitClass {   
         protected override void Awake() {
             AttackRange = 10f;
                 base.Start();
         }
     }
 

Thanks again to @jmonasterio, @hexagonius, and @karl.jones

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

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Update and Awake not being called. 1 Answer

Class c = new Class() okay in class body? 1 Answer

Is OnEnable fired right after Awake? 2 Answers

What is the purpose of this function? "protected virtual void Init() { }" 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