- Home /
Display Name Above Object
I'm trying to figure out how to display the name of a city over the actual city object. It seems as though it should be simple but I seem to be struggling with this.
I create a custom city class (works fine) here.
public class Cities
{
public string cityName { get; set; }
public int cityPrime{ get; set; }
public int cityBasic { get; set; }
public float cityOutput { get; set; }
public Cities (string n, int p, int b, float o)
{
cityName = n;
cityPrime = p; // Prime combat age male population
cityBasic = b; // Population other than Prime Males
cityOutput = o; // Industrial/Production capacity of this city
}
}
Then, I create 10 randomly located PREFAB cities with "Space Filler Names" of "A tempcounter" (Again, this works fine ... I know the city gets named because part of my program allows me to "click" on the city for specific info and the name displays correctly);
void SpawnCity()
{
Instantiate (CityUnit);
CityUnit.transform.position = new Vector3 (Random.Range (75f, 1925f), .03f, Random.Range (75f, 1925f));
CityUnit.GetComponent<Renderer>().sharedMaterial.color = Color.yellow;
string tempName1 = "A " + tempCityCounter.ToString();
int tempOne1 = (int)Random.Range (200f, 300f);
int tempTwo1 = (int) Random.Range (550f, 800f);
city.Add(new Cities(tempName1, tempOne1, tempTwo1, 999));
}
HOWEVER ... I also want to display the name of the city OVER the city object (which is a flat primitive cube). I've been experimenting by adding 3d text as a child component to the City Object but I can't seem to get a specific unique name to display (I can get a generic name such as Cityopolis to display for each and every city but I want the specific/unique name of each city to display over each primitive yellow cube.
I can't quite figure the proper Parent/Child relationship I need (If I even need that) or what/if I need to attach a script to, etc.
Any thoughts would be appreciated.
Answer by MacDx · Sep 28, 2017 at 11:07 PM
When you say "3d text as a child" you mean you added a game object with a TextMesh component, as children to the City prefab game object right? If you did that, then the problem I see is that nowhere in your code is there a reference to said component. What you probably need to do is something like this.
Declare a Text Mesh variable in your CityUnit.cs script.
Go to the prefab in your Project tab, and extend it so you can see the child
Drag the child into the Text Mesh variable in you CityUnit script.
Now every city prefab that you spawn will have a reference to it's own text mesh. After you did that you can go to into your SpawnCity() code and add a line like this at the end.
//cityName would the TextMesh variable delcared inside your CityUnit script
CityUnit.cityName = tempName1;
Now every city should have its own specific name. Hope this helps!
Yes, I believe I tried to add the 3d text as a child just as you say. I also tried to add a Canvas as a child and then text to that, etc. I've tried all sorts of things like this but I'm pretty sure I haven't tried this exact thing that you suggest.
I'm pretty sure it's something in the syntax and/or methodology I'm using that is causing the problem.
I will do what you suggest soon and will let you know my results.
Thanks you.
Sorry, it's late here and I've had guests all evening. I haven't had a chance to look at what you wrote and I probably won't get a chance to review it until morning.
Thanks, again, however. :)
I'm working on this now. Sorry for the delay.
So far, I believe I have steps One through Three of your fix added. I'm still looking at that CityUnit.cityName = tempName1 line.
The bad thing is that right now, All my cities display the same name which is the last placeholder name that I add to my city list. So, if I add 10 cities to my list, all are named "A 9" and if I add 50 cities to my list they are all named "A 49" and so forth.
The good thing is that right now, at least my cities are displaying something other than "Hello World" which is the initial default text. But it usually takes me a while to figure these things out. :)
At least I'm making progress.
That sounds like an issue with tempCity counter. $$anonymous$$ake sure that you are calling your SpawnCity method at the right time.
Thanks for taking the time to assist me. I assign the names when the cities spawn.
I have a method/display that will display information for each city when I "click" on it and it displays the correct name and other data...so I know the cities are getting accurate info to start with.
The thing that I'm not getting correct is the name that is "floating" over the city. They are all the same, which is the name of the last one that spawns.
I'm still working on this between phone calls and other regular life events...I'll stay on it until I can figure it out.
Here's the part that actually "Spawns" the cities.
void SpawnCity()
{
Instantiate (CityUnit);
CityUnit.transform.position = new Vector3 (Random.Range (75f, 1925f), .03f, Random.Range (75f, 1925f));
CityUnit.GetComponent<Renderer>().shared$$anonymous$$aterial.color = Color.yellow;
string tempName1 = "A " + tempCityCounter.ToString();
int tempOne1 = (int)Random.Range (200f, 300f);
int tempTwo1 = (int) Random.Range (550f, 800f);
city.Add(new Cities(tempName1, tempOne1, tempTwo1, 999));
}
Again, the cities are actually created with the correct information. I can verify this by separate code that allows me to "click" on each city and get individual/unique information for each city.
This information I receive from this "click" is correct...this includes the city name which displays accurately when I access via this click.
What is not correct is the "floating" info that appears above each city. In this case, the info that appears/floats above each city is just the default 3d display which initially says "Hello World" .
Hey, I'm making progress. I've got all the cities to change their name from the "default" Text$$anonymous$$esh name to "A 0" which is the name currently assigned to the very first city.
Go Team !!!
I've been going through some of the RTS posts on YouTube (etc). There must be something on point somewhere. I've found a few things regarding floating text but mostly they are too far off base to help in my situation.
I suppose I might need to change bases here soon if I don't come up with something. :)
This prints the city name of the last city in the list...and it prints this last city's name for ALL the cities on the map. I know there is a problem here with my logic/syntax but I'm having a hard time seeing it.
private string cityFloatName;
public Text$$anonymous$$esh myText$$anonymous$$esh;
void Start ()
{
for (int i = 0; i < SpawnAll.city.Count; i++)
{
cityFloatName = SpawnAll.city [i].cityName;
myText$$anonymous$$esh.text = cityFloatName;
}
}
Answer by Topthink · Sep 30, 2017 at 04:52 PM
Here's a bit more that includes the above.
public class SpawnAll : MonoBehaviour
{
public static int friendCounter01 = 0;
public static int enemyCounter01 = 0;
public GameObject RawMaterials;
public GameObject CombatUnit;
public GameObject enemyCombatUnit;
public GameObject CityUnit;
public static List<FriendlyCombatUnit> friendlyCombat = new List<FriendlyCombatUnit> ();
public static List<MilitaryUnit> military = new List<MilitaryUnit> ();
public static List<RawMaterials> rawMats = new List<RawMaterials> ();
public static List<EnemyCombatUnit> enemyCombat = new List<EnemyCombatUnit> ();
public static List<Cities> city = new List<Cities> ();
public static List<int> testList = new List<int> ();
private int tempCityCounter=0;
void Awake ()
{
for (int i = 0; i < 5; i++) {
SpawnFriendlyCombatUnit ();
}
for (int i = 0; i < 5; i++) {
SpawnEnemyCombatUnit ();
}
for (int i = 0; i < 50; i++) {
SpawnRawMaterials ();
}
for (int i = 0; i < 25; i++) {
SpawnCity (); ///
tempCityCounter++;
}
}
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
"Center On Children" programmatically 1 Answer
Attaching a game object as a child during runtime? 3 Answers
Multiple cameras as child or assign coords/rot to main cam 1 Answer
Getting the topmost parent 1 Answer