- Home /
How to use 'check if player is on ground' bool in script
Hi, I'm trying to integrate a bool into a controller script, but I cannot get it to work. The section of the script I'm working with:
void FixedUpdate()
{
//here are actions where the buttons can be held for a longer period
if (mPlayer && mHasControl)
{
if (Input.GetButton("Jump"))
mPlayer.Jump();
mPlayer.Walk(Input.GetAxisRaw("Horizontal"));
}
}
I want to add a bool that is defined in another script:
bool mOnGround = false; //Are we on the ground or not?
So I'm trying to get the line to read:
if (mPlayer && mHasControl && mOnGround)
What's the right way to do this? The reason I'm doing this is because when I jump without moving forward, and then in the air hit the move key... the walk animation plays instead of finishing the jump animation, which is very weird!
Thanks!
I don't see anything wrong with your proposed line - do you get an error?
Yes, it reads: error CS0103: The name 'mOnGround' does not exist in the current context.
Then your problem isn't with the script just with the way you are accessing the variable.
Sorry I didn't catch that you said it's defined in another script. So you have to get a reference to that value in the other script. Check out this link, expand section I // accessing variables in other scripts.
If you have a script for controlling movement then I'd say check if the object is grounded there rather than put it in another script.
Also try setting mOnGround = true
within your script where appropriate so that the if statement can execute it.Since from what I can see you are saying that mOnGround = false
and if it is true then do what is inside here: if()
,yet you haven't specified anywhere that mOnGround
is true.
Answer by DaveA · Aug 27, 2013 at 10:51 PM
var pp : PlatformerPhysics = GetComponent.<PlatformerPhysics();
if (pp.mOnGround .....
or
PlatformPhysics pp = GetComponent<PlatformPhysics>();
Ok, I've added this right under the void Start at the top of the page:
PlatformerPhysics pp = GetComponent<PlatformerPhysics>();
No errors.
But when I try to alter my line below:
if (mPlayer && mHasControl && pp.mOnGround)
I get an error: The name 'pp' does not exist in the current context
In your global class variables (at the top), add
private PlatformerPhysics pp;
Answer by hellaeon · Aug 27, 2013 at 11:17 PM
Sounds like you have the 2 scrips on the same object, and so rightfully expect to access it. However you are missing the scope!
You need to use GetComponent in the local context.
Lets say you have two scripts _A and _B attached to the same object. _A has the variables if mPlayer, mHasControl. _B has the variable mOnGround.
Inside A, to get access to script _B, you need to call the component _B. As it is sitting on the same game object as _A, you should be able to simply type
gameObject.GetComponent<_B>().mOnGround;
This can extend to a further script C:
Say object_one has scripts _A and _B object_two has script _C
_C can access _A or _B by simply :
public GameObject some_object; // attach obejct_one via editor
private bool mOnGround;
void Start()
{
mOnGround = some_object.GetComponent<_A>(). mOnGround;
}
I hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613113110im_/https://answers.unity.com/themes/thub/images/avi.jpg)