Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Topthink · Sep 28, 2017 at 09:27 PM · gameobjecttextparentchild3dtext

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

  1. Declare a Text Mesh variable in your CityUnit.cs script.

  2. Go to the prefab in your Project tab, and extend it so you can see the child

  3. 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!

Comment
Add comment · Show 23 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Topthink · Sep 28, 2017 at 11:29 PM 1
Share

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.

avatar image Topthink · Sep 29, 2017 at 03:15 AM 0
Share

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. :)

avatar image Topthink · Sep 29, 2017 at 10:05 PM 1
Share

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.

avatar image MacDx Topthink · Sep 29, 2017 at 10:19 PM 0
Share

That sounds like an issue with tempCity counter. $$anonymous$$ake sure that you are calling your SpawnCity method at the right time.

avatar image Topthink MacDx · Sep 29, 2017 at 10:46 PM 0
Share

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.

Show more comments
avatar image Topthink · Sep 30, 2017 at 04:53 PM 0
Share

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));
     }
avatar image Topthink Topthink · Sep 30, 2017 at 04:58 PM 0
Share

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" .

avatar image Topthink Topthink · Sep 30, 2017 at 06:10 PM 0
Share

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 !!!

Show more comments
Show more comments
avatar image Topthink Topthink · Sep 30, 2017 at 05:11 PM 0
Share

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. :)

avatar image Topthink Topthink · Sep 30, 2017 at 06:28 PM 0
Share

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;
         }
 
     }
avatar image
0

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++;
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges