Deeper understanding of how Unity Game Objects, Components and Properties relate to one another.
Introduction to [SerializeField].
Creating a reference to a text field.
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…
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!
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.
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?
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.
Just wanted to share what I learned and I really enjoy it
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");
}
}
}
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. :))
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.
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
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.
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.