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

Need UI Buttons to Display Variable Names I.E. Attack1 = Kick or Attack1 = Punch

Discussion in 'UGUI & TextMesh Pro' started by MJG1123, May 5, 2015.

  1. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    I need the text field in my UI Button to call on the value of a variable for the attack the player knows I.E. for Player 1, he currently knows,

    Attack 1 - Punch
    Attack 2 - Kick

    but later he learns a new attack..

    Attack 1 - SuperPunch
    Attack 2 - Kick

    Here's some code online I can't get to work...
    and.. UI Button Text is Variable 1.PNG UI Button Text is Variable 2.PNG FightMenu2.PNG

    upload_2015-5-5_15-15-1.png
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    If you want to update the "Text" for a test component, you will have to only get the Text components in the children.

    E.G.
    Code (CSharp):
    1. gameObject.GetComponentInChildren<Text>();
    Only then can you update the Text.
     
    MJG1123 likes this.
  3. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    So I am then finding the gameobject this script is attached to the plugging in the canvas with the text I need to change? or the text gameobject?

    Screenshot (11).png Screenshot (10).png
     
  4. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    Right, "Attack 1 Text" is the Name of your GameObject where the Text component is attached to. So in your case it would be:
    Code (CSharp):
    1. Var Attack1Text = GameObject.Find("Attack 1 Text").GetComponent<Text>();
    2. Attack1Text.text = "My new text";
    If you intend to do this frequently, it might be better to have a couple of public Text properties in the AttackMenu gameObject, e.g.
    Code (CSharp):
    1. public Text Attack1Text;
    2. public Text Attack2Text;
    Then assign the relevant Text objects to these properties in the editor.

    Then in your script on the AttackMenu, you only need do:
    Code (CSharp):
    1. Attack1Text = "Some attacking text";
    2. Attack2Text = "The uber atack mode";
    Either way will work, but the second option is a lot more performance as you are not using the Find functions, which can be slow.
     
    MJG1123 likes this.
  5. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    Do I need to specify, like "using Unity.Engine.Text" or something because when I type the above it says there's no def for Text
     
  6. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    Lol, sorry yes.

    Either:
    * Add a using statement "using UnityEngine.UI"
    * specify it directly "UnityEngine.UI.Text"

    MonoDevelop or Visual Studio should offer you the option to auto resolve that.
     
    MJG1123 likes this.
  7. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    Didn't work?

    Screenshot (13).png Screenshot (12).png
     
  8. OldManAnimal

    OldManAnimal

    Joined:
    Jul 10, 2014
    Posts:
    45
    Is it possible that the "Attack 1 Text" GameObject is actually named "Attack 1 Text " with an extra space at the end? That would be enough to make the GameObject.Find fail and return null.

    I would suggest not using .Find at all, and you definitely don't want to be using it in Update. If this script that sets the text is attached to the very gameobject the Text component is on you can just use GetComponent directly without the Find.

    Or you could just drag the "Attack 1 Text" GameObject directly from the hierarchy to the public attack1text variable that shows in the inspector and then you can access attack1text.text as much as you want in that script. If you absolutely need to use Find or GetComponent though, best to use them just once in Start or something similar.
     
    MJG1123 likes this.
  9. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    Yup, that was my other suggestion too :D

    Also, it's best not to NAME any gameObject with spaces at all, to avoid confusion :D
     
    MJG1123 likes this.
  10. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    Is it possible to do the same with images..

    like

    public Image image1;

    image1.image = (I don't know what to put here even if possible);
     
  11. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    :D, yes
    You have two options, one for Sprites and one for Textures:

    Code (CSharp):
    1.         Image myImage;
    2.         myImage.sprite = new Sprite();
    3.  
    4.         RawImage myRawImage;
    5.         myRawImage.texture = new Texture();
    Hope that helps :D
     
  12. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    Where is it pulling the sprites/textures from... Im gomna have a large pool to pull from if i can make the game my way...
     
  13. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685
    The sprite can either be one in your asset library, a downloaded resource or a procedurally generated one.
    As you can see in this post, (http://answers.unity3d.com/questions/575521/load-unityenginesprite-from-code.html) there are a lot of ways to load it programatically.

    However, if you don't need this level of flexibility, I'd recommend just having an array of sprites on your script and assign one of those to the particular UI component as you wish. Then assign all the sprites you want in the editor ready for use.
     
  14. MJG1123

    MJG1123

    Joined:
    Apr 12, 2015
    Posts:
    24
    Hopefully last question... how do I reference or call an asset not in the scene?
     
    Last edited: May 11, 2015
  15. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,685