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. Dismiss Notice

UI Button image manipulation problem

Discussion in 'UGUI & TextMesh Pro' started by Fuzzylr, Feb 10, 2015.

  1. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    Greetings,

    I am attempting to make a simple Tic Tac Toe game. I've built the game so far mostly out of UI elements. My thought process here was to originally create the tiles and just simply swap the images on the tiles out. However, every time I attempt to replace the image I keep getting an error. Below I will have the errors along with the code.

    What I have done so far.

    I have built out a panel that is home to a sprite that is game board. I then created a UI Grid layout group with 3 columns. Once I worked out my spacing I created 9 prefab buttons with no sprite / image attached to them so the board displays empty. What I am looking to do is swap the images with either an X or an O on click.

    error
    Code (csharp):
    1.  
    2. Assets/Scripts/GameBoard.cs(16,60): error CS1955: The member `GameBoard.X' cannot be used as method or delegate
    Here is the code from the script

    Code (csharp):
    1.  
    2. publicclassGameBoard : MonoBehaviour
    3. {
    4. //Setupthegameboard
    5. boolgameEnd = false;
    6. publicImageX, O, Blanks;
    7. intsize = 9;
    8. publicGameObject[] squares;
    9.  
    10. //Usethisforinitialization
    11. voidStart()
    12.  {
    13. squares[0].renderer.material.mainTexture = X(typeof(Image));
    14. //Cleartheboard
    15. //clearBoard();
    16.  }
    17.  
    18. //Updateiscalledonceperframe
    19. voidUpdate() {}
    20.  
    21. voidcreateBoard()
    22.  {
    23.  }
    24.  
    25. //Setthegametobeingover
    26. voidgameOver( boolisOver ) { gameEnd = isOver; }
    27.  
    28. /*voidclearBoard()
    29.  {
    30. for(inti = 0; i < size; i++ )
    31.  {
    32.  }
    33.  }*/
    34. }

    If I remove the type cast....
    Code (csharp):
    1. squares [0].renderer.material.mainTexture = X;
    I get this error message....

    Code (csharp):
    1.  
    2. Assets/Scripts/GameBoard.cs(16,47): error CS0029: Cannot implicitly convert type `UnityEngine.UI.Image' to `UnityEngine.Texture'
    3.  
    I have looked pretty hard to find a way to replace the image. I am been unsuccessful. I could really use some suggestions here.
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Not entirely sure where your trip up is going on in that script but to update the image for an Image component on a button, you simply need to:
    Code (CSharp):
    1.     Sprite mySprite;
    2.  
    3.     public void SetImage()
    4.     {
    5.         var myButtonImage = GetComponent<UnityEngine.UI.Image>();
    6.         myButtonImage.sprite = mySprite;
    7.     }
    Note it has to be a sprite for the Image component. If you want to use a Texture, you will need to replace Image with RawImage.
     
    msakkuzu likes this.
  3. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    It is very possible I don't understand the nature of how to work Unity properly. Because now that I have updated the code so that it replaces the image the problem now is it's not getting the right object to replace the image on.

    Code (csharp):
    1.  
    2. //Setupthegameboard
    3. boolgameEnd = false;
    4. publicSpriteX, O, Blanks;
    5. intsize = 9;
    6. publicImage[] squares;
    7.  
    8.  
    9. //Usethisforinitialization
    10. voidStart()
    11.  {
    12. varbuttonImage = GetComponent<UnityEngine.UI.Image>();
    13. buttonImage.sprite = X;
    14. squares[1] = buttonImage;
    15. //Cleartheboard
    16. //clearBoard();
    17.  }

    I am not replacing the image in the box I choose. It's placing the parent objects image.... As you can see the boxes are still white while the board image got replaced via the script.

     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Lol, that script will replace the image of whatever it is attached to.
    If you also want to change the images of the children, then you will need to loop through the children to set their images (or have a script on each child (or their prefab)
    For example:
    Code (CSharp):
    1. for (int i = 0; i < transform.childCount; i++)
    2.         {
    3.             var child = transform.GetChild(i);
    4.             var childImage = child.GetComponent<UnityEngine.UI.Image>();
    5.             childImage = mySprite;
    6.         }
     
  5. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    You are the man. I am going to try to automate the process now. I will move forward with trying to get this working. Thank you very much for you kind assistance.

     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    No worries. Main reason I wrote my UI book was to ensure it helps devs get started with the new system :D
     
  7. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    Ok, I have a lot of stuff working now. I have a simple problem that I am not sure how to resolve. I am trying to show and hide Misc UI elements if the game is over with. However, it is giving me the following errors.

    Error code
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. GameBoard.hideWinnder (Boolean value) (at Assets/Scripts/GameBoard.cs:135)
    4. GameBoard.Update () (at Assets/Scripts/GameBoard.cs:47)
    Here is the code.

    Part 1 - works
    Code (csharp):
    1.  
    2. voidstartGame()
    3. {
    4. //Cleartheboard
    5. clearBoard();
    6.  
    7. //Hidethetext
    8. hideWinnder(true);
    9. hideHighScore(true);
    10.  
    11. //Zerooutvalues
    12. score1 = 0;
    13. score2 = 0;
    14. moves = 0;
    15. gameOver = false;
    16. }

    part 2 - Does not work
    Code (csharp):
    1.  
    2. voidUpdate()
    3. {
    4. pScore1.text = "X: " + score1;
    5. pScore2.text = "O: " + score2;
    6.  
    7. //Checkforwinner
    8. if (!gameOver)
    9. {
    10. checkForWinnder();
    11. }
    12.  
    13. //Ifthegameisover
    14. if (gameOver)
    15. {
    16. //Unhidewinner / Highschoore
    17. hideWinnder(false);
    18. hideHighScore(false);
    19. }
    20. }

    Code definition
    Code (csharp):
    1.  
    2. voidhideWinnder( boolvalue )
    3. {
    4. show = GameObject.FindGameObjectWithTag("Win");
    5. if(value) { show.SetActive(false); }
    6. else { show.SetActive(true); }
    7. }
    8.  
    9. voidhideHighScore( boolvalue )
    10. {
    11. show2 = GameObject.FindGameObjectWithTag ("HighScore");
    12. if(value) { show2.SetActive (false); }
    13. else { show2.SetActive(true); }
    14. }
     
  8. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Most likely cause is that your FindGameObjectWithTag is not finding anything from what I can see.
    If you do a search for an object, ALWAYS test to check if it found something by doing either
    Code (CSharp):
    1. if(show2 ==) Debug.Logwarning("Could not find HighScore");
    Or just:
    Code (CSharp):
    1. if(!show2) Debug.LogWarning("Could not find Highscore");
    Best to always check your references before trying to do something with them! :D