- Home /
Why can't won't my if then statements work?
So this is my code, everyhitng works fine except that after the code executes the part of the "if then" statement it doesn't finish, for example the sprite will change but the Boolean that goes with the sprites won't change nor will the scorecard change. What could I be doing wrong?
UPDATE: The Scorecard changes now but not the Boolean so every Tille becomes a Tic still
UPDATE 2: It apprears the quaestion isn't clear enough, despite referring to a different script the boolean "Turn1" is only being changed on the idvidual Tille. I am going to attempt to add a turn counter of sorts to the refered to script "bol".
UPDATE 3: It works, I added this to the script "bol"
private void OnMouseDown()
{
if(Turn == true)
{
Turn = false;
}
if(Turn == false)
{
Turn = true;
}
and I replaced all instances of "Turn1" with "OSA.Turn". I am someone unsure of any of this code is redundant and would be interested of anyone can tell me why this worked :D. Thank you to those who posted.
public class Tile : MonoBehaviour
{
public Sprite Tille;
public Sprite Tic;
public Sprite Tac;
public int Scorecard;
public GameObject CodeHold;
private Bol OSA;
private bool Turn1;
// Use this for initialization
void Start ()
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = Tille;
OSA = CodeHold.GetComponent<Bol>();
Turn1 = OSA.Turn;
Debug.Log(Turn1);
Debug.Log(Scorecard);
}
private void OnMouseDown()
{
if (Turn1 == true)
{
if (this.gameObject.GetComponent<SpriteRenderer>().sprite == Tille)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = Tic;
Turn1 = false;
Scorecard = 1;
}
}
else if (Turn1 == false)
{
if (this.gameObject.GetComponent<SpriteRenderer>().sprite == Tille)
{
this.gameObject.GetComponent<SpriteRenderer>().sprite = Tac;
Turn1 = true;
Scorecard = -1;
}
}
}
}
When where is your On$$anonymous$$ouseDown() called? If you wanted this to happen when you click you need to check for Input.Get$$anonymous$$ouseButtonDown(0) which is true during single frame when left mouse button is pressed.
This might be the answer you need:
private void Update()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
On$$anonymous$$ouseDown();
}
}
While that is an interesting idea this makes it so that all tille are treated as one and that can't work, I appreciate the help though.
{
//Let's assume Turn = True.
if(Turn == true) //Turn is True, so...
{
Turn = false; //$$anonymous$$ake it false. Turn is now False.
}
if(Turn == false) //Since Turn is now False, do this too.
{
Turn = true; //Turn is now True.
}
In this snippet, no matter what, Turn will ALWAYS be True On$$anonymous$$ouseDown.
However this isn't the answer because your latter code makes it an Else If.
Answer by OusedGames · Feb 16, 2017 at 02:24 PM
Hello,
In both if statements you tried to get "sprite == Tille", is that right?
Because you changed the sprite to - Tic and Tac
Shouldn't it look for the new sprites?
Delete the inside if line (sprite == Tille) and see if it works
So that does kinda work but the whole idea is that every other click should give a different sprite, but once a sprite as been given it can't be changed, while what you recommended does allow the sprites to be changed more than once. I appreciate your help regardless :)