- Home /
Can`t regulary send bool value from one script to another after disabling gameobject and enabling again
When i debug it before disabling gameobject, its ok, but when i disable and enable and trying to jump, in original scripts it shows right number of space pressing, in second scripts only 1/5 or less goes true(p.s also tried fixed upd)
public bool jump = false;
void Update () {
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
Debug.Log(jump+"1");
}
void FixedUpdate()
{
controller.Move(horizontalMove * Time.deltaTime ,false,jump);
jump = false;
}
another script
public PlayerMovement playerMovement;
void Update()
{
isJumping = playerMovement.jump;
Debug.Log(isJumping);
}
Answer by Casiell · Feb 05, 2019 at 06:01 PM
For the life of me I can't see what you are trying to achieve with this script. But your mistake is quite simple. You set your "jump" bool to true in Update and to false in FixedUpdate. You rely on your PlayerMovement script Update to happen after your first script Update, or between first script Update and FixedUpdate.
I can't say if you will understand my previous sentence (I don't think I can) so I will skip the details now and just point out the mistake: you rely on script execution order to be exactly the way you want. Never ever do that. Like ever, in 99,99% cases there is a better way.
From the get go, I would say that you should create an event and call it every time you press space. Then from other scripts you just subscribe to this event and your functions will be called whenever you invoke the event aka press space
it's simple movement script where i need to check where my player should go, and when i press space jump goes true, in another way its always stay false the question is not about how to write scripts, but why its not send(or send it rarely) true after enabling to another script
p.s. i add event with my animation, its always works fine , but i dont want to play animation in movement script
Your answer
Follow this Question
Related Questions
Disable, Enable a User Input? 1 Answer
This should be simple : Input Button Question 1 Answer
enable/disable image effects in code 1 Answer
Getting an objects Children 1 Answer
Disable a GameObjects scripts knowing only the GameObjects name 3 Answers