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…
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.
gameObject.GetComponentInChildren<Text>();
Only then can you update the Text.
1 Like
SimonDarksideJ:
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.
gameObject.GetComponentInChildren<Text>();
Only then can you update the Text.
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?
Right, “Attack 1 Text” is the Name of your GameObject where the Text component is attached to. So in your case it would be:
Var Attack1Text = GameObject.Find("Attack 1 Text").GetComponent<Text>();
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.
public Text Attack1Text;
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:
Attack1Text = "Some attacking text";
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.
1 Like
SimonDarksideJ:
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.
public Text Attack1Text;
public Text Attack2Text;
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
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.
1 Like
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.
1 Like
Yup, that was my other suggestion too
Also, it’s best not to NAME any gameObject with spaces at all, to avoid confusion
1 Like
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);
, yes
You have two options, one for Sprites and one for Textures:
Image myImage;
myImage.sprite = new Sprite();
RawImage myRawImage;
myRawImage.texture = new Texture();
Hope that helps
, yes
You have two options, one for Sprites and one for Textures:
Image myImage;
myImage.sprite = new Sprite();
RawImage myRawImage;
myRawImage.texture = new Texture();
Hope that helps
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…
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, (Load UnityEngine.sprite from code. - Questions & Answers - Unity Discussions ) 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.
[“simonDarksideJ, post: 2105483, member: 115457”]
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, (Load UnityEngine.sprite from code. - Questions & Answers - Unity Discussions ) 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.
Hopefully last question… how do I reference or call an asset not in the scene?