Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Button Problem

Discussion in 'Scripting' started by markzareal, Aug 5, 2014.

  1. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Firstly, I asked this on unity answers and got too many answers and comments that were not so helpful, so I decided to post this on a forum where we can discuss.

    This is my script:
    Code (JavaScript):
    1. var targetScript : ChangeSprite;
    2. var AcceptInput : boolean = true;
    3. private static var score : int = 0;
    4. var guiScore : GUIText;
    5. function Start ()
    6. {
    7. guiScore.text = "Score: 0";
    8. }
    9. function OnMouseDown () {
    10. if(!AcceptInput)
    11. {
    12. return;
    13. }
    14. AcceptInput = false;
    15. targetScript = GameObject.Find("Shape").GetComponent(ChangeSprite);
    16. GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = true;
    17. Debug.Log("Clicked");
    18.  
    19. if(targetScript.spriteRenderer.sprite == targetScript.diamond) {
    20. AcceptInput = true;
    21. score += 1;
    22. guiScore.text = "Score: " + score;
    23. }
    24. else{
    25. Debug.Log("Wrong Answer!");
    26. }
    27. }
    28. function OnMouseUp () {
    29. AcceptInput = true;
    30. GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = false;
    31. }
    32.  
    What the script does is when the button is pressed, it will activate the other script which would change the sprite of another object. And if the sprite is diamond, it would add a score and if not, Debug.Log("Wrong Answer!"). The problem is, if I press the button, the score would add even before the sprite was even displayed on the screen. Is there any way to make the button only work when the sprite is displayed? I tried the wait for seconds method but it wasn't really helpful. what it did was it added the score a second later but it was still the same. Does anyone have any ideas? Any help would be much appreciated. Thanks in advance!
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    First of all, use tabs. Your code is unreadable right now. Second, using Find is expensive and should be avoided if you use it a lot. Third, having a boolean AcceptInput is unneeded. You can only have one downpress at a time.
     
  3. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Code (JavaScript):
    1. var targetScript : ChangeSprite;
    2. var AcceptInput : boolean = true;
    3. private static var score : int = 0;
    4. var guiScore : GUIText;
    5.  
    6.  
    7. function Start ()
    8. {
    9. guiScore.text = "Score: 0";
    10. }
    11.  
    12.  
    13. function OnMouseDown () {
    14. if(!AcceptInput)
    15. {
    16. return;
    17. }
    18. AcceptInput = false;
    19.  
    20.  
    21. targetScript = GameObject.Find("Shape").GetComponent(ChangeSprite);
    22.  
    23.  
    24. GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = true;
    25. Debug.Log("Clicked");
    26.  
    27. if(targetScript.spriteRenderer.sprite == targetScript.diamond) {
    28. AcceptInput = true;
    29. score += 1;
    30. guiScore.text = "Score: " + score;
    31. }
    32.  
    33.  
    34. else{
    35. Debug.Log("Wrong Answer!");
    36. }
    37.  
    38. }
    39.  
    40. function OnMouseUp () {
    41. AcceptInput = true;
    42. GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = false;
    43. }
    44.  
    I used the accept input because if I don't, it would flash through the sprites unless I release. And I don't get why GameObject.Find is expensive, can you elaborate on it?
     
  4. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
     
  5. AxisRob

    AxisRob

    Joined:
    Aug 6, 2013
    Posts:
    33
    Please use tabs in your code. Having it all in one straight line (see how the left-hand side is straight instead of staggered like it is in monodevelop or visual studios) makes this very time-consuming to read.

    Secondly, when you run Find, it takes up a good bit of processing power. Not enough to cause a difference if you're running it infrequently, but if you run it often (or a lot in a single frame), you will probably see stuttering.

    You have:
    Code (CSharp):
    1. targetScript = GameObject.Find("Shape").GetComponent(ChangeSprite);
    2.  
    3. GameObject.Find("Shape").GetComponent(ChangeSprite).enabled = true;
    Would this not do (assuming you cannot just assign the script you want in the inspector)?

    Code (CSharp):
    1. targetScript = GameObject.Find("Shape").GetComponent(ChangeSprite);
    2. targetScript.enabled = true;
    You already found and cached the sprite you want; you should be good to go.

    Finally, OnMouseDown should only be called once per click (that I am aware). If it's rapidly cycling sprites, that is... something strange.
     
  6. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    Because it makes the disabled script true when OnMouseDown, I have to use OnMouseUp to make it false again. If I don't use the accept input thing, it would just rapidly flash through. The main problem is I want the button only work if the sprite is displayed. Otherwise it would display the score even before the sprite is displayed on the screen. Thanks for your time
     
  7. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
     
  8. markzareal

    markzareal

    Joined:
    Jun 11, 2014
    Posts:
    40
    maybe I need some kind of delay to restrict the button