Player cannot jump if not in update or fixed update

I’m almost new in unity but not so noob.
if I call this in FixedUpdate or Update, it works perfectly.
This is my code:

 void ChickenJump(){
                    print ("touch");
    		if (Grounded&&Input.GetKeyDown(KeyCode.Space)) {
    			chickenbody.velocity = new Vector2 (0.0f, jumpRange);
    		}

but if I use this code and called with a button, it does’t work!

public void ChickenJump(){
                print ("touch");
		if (Grounded) {
			chickenbody.velocity = new Vector2 (0.0f, jumpRange);
		}
}

The print is showing, meaning it is called, but the jump does’t work!

please help…

And is Grounded true?

How do you check if object is grounded?

2 Answers

2

Grounded is not true then. Are you using charactercontroller? because if not you should since it contains a check for player being grounded and you don’t need to do it yourself.

Here is the thing - all of my settings classes need to have MonoBehaviour because the game can edit their values on the fly. That is the problem - I want to know if I can directly serialize a MonoBehaviour class or at least without using a special clone class as a container and a middleman. I partially understand why you can't serialize MonoBehaviour classes, but I need to only change the values, not references, and doing it with a container class and a function which transfers values seems redundant.

Ok, I recreated the script in a new project. I only try that specific problem… it works, and it jump smoothly like i wanted to. I don’t know why, this happened to me every time. I think something is wrong with my Unity application.

I don't understand why the way I suggested wouldn't work for you since it doesn't matter if the values are in the main body of a MB script, or part of a class instance referenced by a variable inside the MB script. Nevertheless, you could also write SerializationSurrogates for your MB scripts that should be serialies but that would also mean having to access each member directly. Long story short: If you want to serialize, don't inherit from MB.