How to declare variables in class derived from other class variables in the same class?
I have the following code, but it doesn't work right. In particular, the vector2 XY does not appear to be returning the proper values. I want XY to be the vector2 form of the coordinates (X, Y) from the class. The first constructor is the one that I am interested in; the others seem to work (but do they !?)...
class OneRoom {
public int X;
public int Y;
public int PortalConfig;
public int BaseLevel;
public Vector2 XY;
public OneRoom(int SetX, int SetY, int SetPortalConfig, int SetBaseLevel) {
X = SetX;
Y = SetY;
XY = new Vector2((float)SetX, (float)SetY);
PortalConfig = SetPortalConfig;
BaseLevel = SetBaseLevel;
}
public OneRoom(Vector2 SetXY, int SetPortalConfig, int SetBaseLevel) {
X = (int) SetXY.x;
Y = (int) SetXY.y;
XY = SetXY;
PortalConfig = SetPortalConfig;
BaseLevel = SetBaseLevel;
}
public OneRoom(OneRoom FromRoom){
X = FromRoom.X;
Y = FromRoom.Y;
XY = FromRoom.XY;
PortalConfig = FromRoom.PortalConfig;
BaseLevel = FromRoom.BaseLevel;
}
I have tested this and found that XY.x and X are never the same, for example XY.x = -20 and X = 80.
$$anonymous$$ight be this problem (although you're not working on a script component in this case so I'm not sure..): The unity scripting manual says the following about using constructors inside unity:
"Note to experienced programmers: you may be surprised that initialization of an object is not done using a constructor function. This is because the construction of objects is handled by the editor and does not take place at the start of gameplay as you might expect. If you attempt to define a constructor for a script component, it will interfere with the normal operation of Unity and can cause major problems with the project."
Are you sure you're not accidentally assigning it in an "if" statemant for example in another script? Like this: if (XY.x = 0) ... That's the only thing i can think of.