Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LegitTeddyBears · Feb 16, 2017 at 01:59 PM · variablevariablesif-statementsintegerif else

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;
             }
         }
     }
 }
 











Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kossuranta · Feb 16, 2017 at 02:13 PM 0
Share

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();
     }
 }
avatar image LegitTeddyBears Kossuranta · Feb 16, 2017 at 10:40 PM 0
Share

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.

avatar image CarterG81 · Feb 16, 2017 at 11:07 PM 0
Share
  {
 //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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LegitTeddyBears · Feb 16, 2017 at 10:36 PM 0
Share

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 :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to increase score for objects that form larger geometries? 0 Answers

Problem with variables,Problem with identifying a variable 2 Answers

if statements is not working properly 1 Answer

Object doesn't move 2 Answers

What makes a prefab alter their variables at runtime? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges