About 'Update Text Component'!

In this video (objectives)…

  1. Deeper understanding of how Unity Game Objects, Components and Properties relate to one another.
  2. Introduction to [SerializeField].
  3. Creating a reference to a text field.
  4. Printing a string to our UI text field.

After watching (learning outcomes)…

Capable of programmatically adding a string of text to a UI text field.

(Unique Video Reference: 5_TX_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

The timing of the challenges are perfect. Just enough information is given that we have to take a bit of time to make something that we can appreciate. Giving us a challenge is a great idea cause I always find myself trying to go a little “above” the “requirements” of the challenge, and see how the results look!

I want to add that you two really make the course fun, and its slightly addicting to want to come back to see how far I can progress! It’s so awesome to see everyone’s results, and the diversity of everyone’s imagination! Thanks for everything!

1 Like

I can’t view the lecture 21 or 22.

Both updating text Components and game states when trying to watch goes to straight to lecture 23 talking about the new content and old.

Thanks for the note @adam_y2005, I appreciate you flagging this for us. Thankfully, this one is a known issue - Udemy has been having troubles with video uploads for the past 18 hours or so, they claim its resolved and I’m reuploading… so hopefully all should be working within the hour.

Thanks Rick i can now view the videos

1 Like

I’m wondering why you had brackets around the string towards the end of the lecture, (“I am added programmatically!”)? I tried the same thing without the brackets and it seemed to work fine. Is this a formatting preference, or will not having brackets cause problems down the line?

Loving these lessons, by the way!

The main reason for the brackets is to create good habits so that we can easily join elements without things going astray. For example, we could print ("your score is: " + scoreVariable) and it would print out something like “your score is: 12”. Without the parentheses it wouldn’t work.

1 Like

Sounds like a good habit to create. Thanks for the help! :slight_smile:

1 Like

Doesn’t [SerializeField] work the same as writing “public” before the variable? Sorry for the very noob question :sweat_smile:

Edit: Nevermind! Found the answer in the Q&A for the lecture :slight_smile:

1 Like

Just wanted to share what I learned and I really enjoy it :slightly_smiling_face:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class AdventureGame : MonoBehaviour {

	[SerializeField]
	Text textComponent;

	// Use this for initialization
	void Start () {
	
		textComponent.text = ("I am Added by myself");
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetKeyDown (KeyCode.UpArrow)) {
			textComponent.text = ("its time to sleep");
		}
		if (Input.GetKeyDown (KeyCode.DownArrow)) {
			textComponent.text = ("its time to go");
		}
		if (Input.GetKeyDown (KeyCode.Space)) {
			textComponent.text = ("its time to jump");
		}
	}
}
1 Like

This does work without the parentheses, though - the following compiles fine and produces the result I’d expect: “Welcome to the #1 adventure game.” Is using brackets when concatenating strings just a style choice? I’ve never seen them used like this before, but I also don’t have much C# experience.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AdventureGame : MonoBehaviour {

    [SerializeField]
    Text textComponent;

    int gameRank = 1;

	// Use this for initialization
	void Start () {
        textComponent.text = "Welcome to the #" + gameRank + " adventure game.";
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

(Thank you for fixing my code formatting, Rick - I wasn’t sure how to do that. :))

Why do you use the brackets for represent a string?

textComponent.text = ("Hello World");

What is the different without brackets?

textComponent.text = "Hello World";

I had a problem with the latest version of Unity (2018.1.6f1). When trying to assign the Text Component I first had to drag them to the Asset Window. If I didnt do this, Unity wouldnt let me assign Story Text to the Text Component Field in the Inspector Window. I dont know if its just me or if its something new in Unity.

Two characters less typed :slight_smile: No need for the brackets.

Can you pop a screenshot up, or a short video of the issue Dave?

Hope this helps

Hi Dave,

Sorry, that image isn’t very clear, photobucket also takes a very long time to load due to all the adds and appears to lack a zoom feature.

Could you just copy/paste your screenshot into your post here instead, easier all round.

Thanks :slight_smile:


Updated Sat Aug 18 2018 15:49

Thanks for making the change.

I’ve not looked at this updated version of the section yet, but I’m assuming that the Game GameObject has the AdventureGame.cs script attached to it - correct?

If so, you would need to select the Game GameObject in the Hierarchy, and then drag across the Story Text GameObject from the Hiearchy into the exposed Text Component field within the Inspector.

At the moment, in your screenshot, it looks like you are selecting the AdventureGame.cs script and then trying to add the Story Text component that way.

Hi Rob,

Thx for the help. Think I better go over the video again as I obviously missed a step somewhere.

1 Like

No worries, let us know how you get on :slight_smile:

Been through it again and it works fine. I was at fault first time. Think id better have more sleep behind me before continuing. Or maybe stronger coffee. Thx for the time and help.

1 Like

Privacy & Terms