- Home /
Inherit functions that work properly
Sorry for my bad english.
I've created a parent class that I expect to have all functions related to test if the gameObject is grounded, in water, on air, etc... given that this functions will be used by the player as well as other gameObjects. However, the child class seems not to beig inherit properly the functions.
The parent script it's as follows:
public class CharacterPhysic : MonoBehaviour {
[SerializeField] protected Transform groundPoints;
float grounRadius;
private void Start ()
{
whatIsGround = LayerMask.GetMask("Ground");
groundRadius = 0.01f;
}
protected bool IsGrounded()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(groundPoints.position, groundRadius, whatIsGround);
if (colliders.Length > 0)
{
return true;
}
else return false;
}
private void FixedUpdate()
{
Debug.Log(IsGrounded());
}
}
And the children script is just
public class ErrantMove : CharacterPhysic {
private void FixedUpdate()
{
Debug.Log(IsGrounded());
}
}
When added the first script as component to a gameobject (after define the grounPoint) the Debug.Log(IsGrounded());
returns TRUE
However, when added the second script as component to the same gameobject (after define the grounPoint and remove the first script) the Debug.Log(IsGrounded());
returns FALSE even in the same circumstances.
I'm expecting to be able to add movement functions into the second script but this depend on the ability to test if it is grounded.
Answer by A_Lego · Oct 31, 2018 at 05:18 AM
"whatIsGround = LayerMask.GetMask("Ground");" is being run in a PRIVATE start function, which can only be accessed by the base/main class and not children/derived classes. So your basically just not setting the layermask in the Physics.OverlapCircleAll function! Also: IsGrounded can't really be used because the values you requesting from it, like "grounRadius", are private variables to the parent "CharacterPhysic" class.
I may be mistaken on how inheritance works, but I believe the scope of your variables is the issue nonetheless
Your answer
Follow this Question
Related Questions
C# Inheritance Problems 1 Answer
Inheritance - Need Both Scripts Attached? 1 Answer
Inheritance with enemies 1 Answer
Accessing variable from parent 1 Answer