- Home /
Debug.Log(); question.
i have a a bool function:
public bool Jump()
{
if (m_Controller.isGrounded)
{
m_MotorThrottle += new Vector3(0, MotorJumpForce, 0);
return true;
}
else
{
return false;
}
}
and im trying to check whether it is true or not with Debug.Log(Jump()); but every time Debug log is called it causes the Jump function to run. and i don't know how to use print. Will print cause the same issue?
To check whether a function is returning true
or false
, the function have to be run first to capture the return value
.
If you are talking about m_Controller.isGrounded
, then just use Debug.Log(m_Controller.isGrounded)
.
Or are you looking for
public bool Jump()
{
if (m_Controller.isGrounded)
{
Debug.Log( true );
m_$$anonymous$$otorThrottle += new Vector3(0, $$anonymous$$otorJumpForce, 0);
return true;
}
else
{
Debug.Log( false );
return false;
}
}
i want to call debug log in update so it runs every frame.
Edit: nvm i figured it out.
Answer by Owen-Reynolds · Mar 17, 2013 at 04:03 PM
You certainly can call Debug.Log on functions, for example, Debug.Log(Mathf.magnitude(p1-p2));
or your own: Debug.Log(nearestEnemy())'
. Nothing wrong with that.
In formal terms, Jump has a side-effect. It figures out if you can jump and makes you jump. So, of course calling it jumps you. Formal programming says to split into bool canJump()
and void doJump()
. For longer stuff, that can sometimes help avoid confusion -- is the function computing numbers for me, or doing something?
This wasn't my solution, but I forgot the double parentheses, so I wrote it as Debug.Log(GroundCheck); and not Debug.Log(GroundCheck()); Dumb, I know, but this could be a common solution for people who aren't used to writing variable methods