Identical code presenting different results
I have been following through the Tanks! tutorial and seem to have hit a snag. Below are two blocks of code, which through all tools I have used appear as though they should compile identically. However, the first does NOT update the =_AimSlider.value but the second does... If I copy and paste JUST the Update function from 2 to 1, it works... the second example is the pre-completed version so I REALLY want to know what I have done wrong. I THINK it is an underlying linespace issue but I get no warnings and can't fix it :/.
My version
private void Update() { // Track the current state of the fire button and make decisions based on the current launch force. m_AimSlider.value = m_MinLaunchForce; // Set slider value to minimum possible initially.
if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired) // At max charge, not yet fired... { m_CurrentLaunchForce = m_MaxLaunchForce; // So we set the launch force to the maximum so it cannot be over max charge. Fire(); // Then call the fire function to fire the shell. } else if (Input.GetButtonDown(m_FireButton)) // Have we pressed fire for the first time? { m_Fired = false; // We know the button has just been pressed to we have not fired yet. m_CurrentLaunchForce = m_MinLaunchForce; // We also know that the launch force will (or should) be at it's minimum value, so we set that as well. m_ShootingAudio.clip = m_ChargingClip; // Sets the current audio clip to play, as the charging clip as we have just started holding shoot (hence charging a shot). m_ShootingAudio.Play(); // Play the charging sound. } else if (Input.GetButtonDown(m_FireButton) && !m_Fired) // Holding the fire button but not yet fired. { m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime; // Set the current launch force to equal it's current value plus charge speed * time held. m_AimSlider.value = m_CurrentLaunchForce; // Update the arrow shape/size to match the current launch force. } else if (Input.GetButtonUp(m_FireButton) && !m_Fired) // We released the button, having not fired yet. { Fire(); // Fire button has been released so we simply call the fire function. } }
Example version
private void Update () { // The slider should have a default value of the minimum launch force. m_AimSlider.value = m_MinLaunchForce;
// If the max force has been exceeded and the shell hasn't yet been launched...
if (m_CurrentLaunchForce >= m_MaxLaunchForce && !m_Fired)
{
// ... use the max force and launch the shell.
m_CurrentLaunchForce = m_MaxLaunchForce;
Fire ();
}
// Otherwise, if the fire button has just started being pressed...
else if (Input.GetButtonDown (m_FireButton))
{
// ... reset the fired flag and reset the launch force.
m_Fired = false;
m_CurrentLaunchForce = m_MinLaunchForce;
// Change the clip to the charging clip and start it playing.
m_ShootingAudio.clip = m_ChargingClip;
m_ShootingAudio.Play ();
}
// Otherwise, if the fire button is being held and the shell hasn't been launched yet...
else if (Input.GetButton (m_FireButton) && !m_Fired)
{
// Increment the launch force and update the slider.
m_CurrentLaunchForce += m_ChargeSpeed * Time.deltaTime;
m_AimSlider.value = m_CurrentLaunchForce;
}
// Otherwise, if the fire button is released and the shell hasn't been launched yet...
else if (Input.GetButtonUp (m_FireButton) && !m_Fired)
{
// ... launch the shell.
Fire ();
}
}
Answer by jmonasterio · Jan 01, 2016 at 05:16 PM
Code is not the same in both.
In the first one, line 16 calls GetButtonDown()
But in the second one, line 20 calls GetButton()
You could generally resolve this issue yourself by using a program called a DIFF utility. A good example for windows is WinDiff, but there are many free and available for purchase.
Believe it or not I did run this through WIN$$anonymous$$erge. Perhaps I wasn't using it properly or WinDiff is better for this as Win$$anonymous$$erge returned no differences between the files. This was a great lesson in the obscurity of debugging (certainly for a beginner) and how important it is to know how to use the correct tools.
Thank you so much for your help. :)
No problem. Here is a related tip: I'd avoid comments on the end of lines like...
Fire(); // Then call the fire function to fire the shell.
If you always put comments on a separate line, your diffs will be easier to read (especially if you just changed comment, and didn't change code on that line).
The "original" code didn't have those comments, but it looks like you added comments to help you understand (which can be helpful). Just don't do end of line comments and you'd have quickly seen which lines of code changed.
Or (second best) get a diff tool that can ignore comments and whitespace.
Great advice. Thanks so much, I'm always happy to take on board any experience no matter how small.
Your answer
Follow this Question
Related Questions
Tanks Tutorial Image separating 0 Answers
problem with tank tutorial 1 Answer
OverslapSphere dont detect enemyhealth 0 Answers
How to create a Cookie Clicker (idle) game clone? 1 Answer
Error CS0246 in GameManager (Roguelike 2D tutorial) 1 Answer