- Home /
Classes and type casting?
enum ModTypes { DoT,Blindness }
class Modifier { var type = ModTypes.DoT; var magnitude : float; var duration : float; var timer : float = 0.0;
var activateTimer = 0.0; var activateInterlapse; } public var modifiers = new ArrayList();
function ProcessModifiers() { for (mod in modifiers) { mod.timer += Time.deltaTime; if (mod.type == ModTypes.DoT) {
...
On that last line I receive a: "Assets/Scripts/Networking/PlayerScript.js(139,30): BCE0051: Operator '==' cannot be used with a left hand side of type 'System.Object' and a right hand side of type 'ModTypes'." Even though it is obviously the correct type?
Answer by duck · May 22, 2010 at 06:21 PM
When pulling items out of an ArrayList (or a Javascript Array() class), you usually need to re-cast it back to the correct type.
If you're iterating through the array, you can do it by declaring the iterator variable's type explicitly, like this:
for (var mod : Modifier in modifiers) {
or you can cast on-the-fly like this:
if ((mod as Modifier).type == ModTypes.DoT) {
Hopefully one of these will solve your error.
Answer by Mike 3 · May 22, 2010 at 06:21 PM
It looks like it's another case of javascript's type inference not being able to determine the type
if you change it to
var type : ModTypes = ModTypes.DoT;
it should work
Your answer
Follow this Question
Related Questions
if statement inside variables of a class 1 Answer
[C#]how to use child class type in parent class method. 1 Answer
Access objects enum type 1 Answer
C# Weapon class generation 2 Answers
EnumPopup in Javascript? 2 Answers