- Home /
The question is answered, right answer was accepted
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;
}
}
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?
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
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.
additional guess: you accidentally added the BaseUnitClass script to the same or another object in the same scene.
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.
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.
do you have both classes in the same script file? If so separate them.
I think it is possible to have more than one class in a file. Perhaps not common, but it certainly doesn't affect anything.
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