This is just something I’m curious about. (sorry if I don’t use all the appropriate technical terms)
In a parent class, I have a method that compares some float and Vector2 positions/amounts (set in the Awake method, aside from transform.position and mouse position/mousePos) in world space to see if the mouse is outside a certain area (IsMouseOutsideDeadzone(), returning true or false). This works fine in the parent class.
I have a child class which inherits from the parent class. In this child class I can access the inherited variables and methods - “horzExtent”, “deadzoneHalves”, IsMouseOutsideDeadzone() method, etc.
However, inside the child class, the float and Vector2 variables (which, in the parent class, had values applied to them) are set to 0. I have access to the variable names+types inside the child class but not the amounts set in the parent class. (The exceptions are transform.position and mousePos mentioned above; mousePos is running in the parent class’ Update method edit: mousePos is static, I forgot)
To be more specific, in the parent class, IsMouseOutsideDeadzone() compares mousePos to a transform.position x and y values + or - deadzoneXHalf or deadzoneYHalf. When calling IsMouseOutsideDeadzone in Update in the parent class, I get what I’m expecting; however when calling IsMouseOutsideDeadzone in the child class, while transform.position and mousePos return the right amounts, all the variables created in the parent class set in Awake return 0.
//PARENT class
protected float horzExtent, vertExtent;
protected Vector2 cameraBoundsSize, deadzoneBoundsSize;
protected Vector2 deadzoneHalves;
...
void Awake()
{
//get the extents (half the vert and horiz cam sizes)
horzExtent = Camera.main.orthographicSize * Screen.width / Screen.height;
vertExtent = Camera.main.orthographicSize;
//store the bounds of the camera (extents * 2)
cameraBoundsSize = new Vector2(horzExtent * 2, vertExtent * 2);
//create the deadzone size
deadzoneBoundsSize = new Vector2(horzExtent, vertExtent);
deadzoneHalves = new Vector2(horzExtent / 2, vertExtent / 2);
}
void Update () {
//get mouse pos
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//if mouse is outside the deadzone areas of the camera
if (IsMouseOutsideDeadzone())
{
//do stuff
}
}
protected bool IsMouseOutsideDeadzone()
{
float tranPosX = transform.position.x;
float tranPosY = transform.position.y;
float deadzoneXHalf = deadzoneHalves.x;
float deadzoneYHalf = deadzoneHalves.y;
//if mouse is outside deadzone
if (mousePos.x > tranPosX + deadzoneXHalf || mousePos.x < tranPosX - deadzoneXHalf ||
mousePos.y > tranPosY + deadzoneYHalf || mousePos.y < tranPosY - deadzoneYHalf)
{
//Debug.Log("mouse is outside deadzone");
return true;
}
return false;
}
---------
//CHILD class, inheriting from parent
void Start()
{
Debug.Log(transform.position); //gets camera transform position
//the following all return "0", unlike in the parent class
Debug.Log("horzExtent: " + horzExtent);
Debug.Log("deadzoneHalves: " + deadzoneHalves);
Debug.Log("camBoundsSize: " + cameraBoundsSize);
}
// Update is called once per frame
void Update () {
Debug.Log("MOUSE: " + mousePos); //DOES return the mouse position in world space, like in the parent class
//if mouse is outside deadzone, get the amount it is outside deadzone, and use that as amount to move
SmoothDampToMousePos();
}
void SmoothDampToMousePos()
{
//unlike in the parent class, here basically always returns "true" because all deadzone values are "0"/there is no area where the mouse is not recognized
if (IsMouseOutsideDeadzone())
{
Debug.Log("outside deadzone, do stuff");
}
I assume this is standard behavior and has to do with child classes inheriting the associated variable names + types of the parent variables, but not their respective values (after the equal sign) set in the parent class; or something like that (objects/instances/OOP? are value vs reference types involved here? :p). I figured I’d ask about it.
(I don’t think I’ll be using inheritance here, doesn’t seem quite right for what I’m doing anyway). Thanks for any further info in advance.