Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Daphnis26 · Aug 27, 2013 at 09:59 PM · c#animationmovementboolean

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!

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Aug 27, 2013 at 10:02 PM 0
Share

I don't see anything wrong with your proposed line - do you get an error?

avatar image Daphnis26 · Aug 27, 2013 at 10:08 PM 0
Share

Yes, it reads: error CS0103: The name 'mOnGround' does not exist in the current context.

avatar image thaiscorpion · Aug 27, 2013 at 10:18 PM 0
Share

Then your problem isn't with the script just with the way you are accessing the variable.

avatar image getyour411 · Aug 27, 2013 at 10:18 PM 0
Share

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.

avatar image gamerant · Aug 27, 2013 at 10:21 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DaveA · Aug 27, 2013 at 10:51 PM

 var pp : PlatformerPhysics = GetComponent.<PlatformerPhysics();
 
 if (pp.mOnGround .....


or

 PlatformPhysics pp = GetComponent<PlatformPhysics>();
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Aug 27, 2013 at 10:53 PM 0
Share

OP probably we'll need a C# translation

avatar image Daphnis26 · Aug 27, 2013 at 11:07 PM 0
Share

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

avatar image getyour411 · Aug 27, 2013 at 11:11 PM 0
Share

In your global class variables (at the top), add

 private PlatformerPhysics pp;
avatar image Daphnis26 · Aug 27, 2013 at 11:15 PM 0
Share

Hmm, unexpected symbol 'private'

avatar image getyour411 · Aug 27, 2013 at 11:18 PM 1
Share

Change it to public

Show more comments
avatar image
1

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

mystery RestartLevel function used in official tutorial 2 Answers

No movement when animation plays (2d) 1 Answer

My animation keeps on repeating on my animator 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges