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 /
  • Help Room /
avatar image
0
Question by dhaggerfin · Jun 08, 2016 at 03:44 PM · variabletextmesh

Setting Text Mesh with a variable

I'm just starting unity and making a card game. I have a Quad GameObject "CardQuad" with a bunch of child objects, some of which are TextMesh objects that will represent the card name, abilities, health, and attack of the card. I am trying to set the TextMesh in Update(). I can set it to a quoted string, but when I try to set it to a string variable it comes out as blank when I hit play.

I've set my public variables in the UI.

Debug.Log shows:

 "Rampaging Rhino of the Jungle
 UnityEngine.Debug:Log(Object)
 CardCreator:Start() (at Assets/CardCreator.cs:19)" 

So I know my string assign is working.

Why is it not setting the TextMesh.text to my variable though?

 using UnityEngine;
 using System.Collections;
 
 public class CardCreator : MonoBehaviour {
 
     public string prefix;
     public string suffix;
     public string creatureType;
     public int attack;
     public int defense;
     public string abilities;
     public char color;
 
     string cardName;
 
     // Use this for initialization
     void Start () {
         cardName = prefix + " " + creatureType +  " " + suffix;
         Debug.Log (cardName);
     }
     
     // Update is called once per frame
     void Update () {
         GameObject g = GameObject.FindGameObjectWithTag ("CardName");
 
         TextMesh t = g.GetComponent<TextMesh> ();
         Debug.Log ("Name = '" + t.text + "'");
         t.text = "some new string";  //this works and updates the card name
         t.text = cardName;   //this makes the card name blank
     }
 }
 
Comment
Add comment · Show 2
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 FortisVenaliter · Jun 08, 2016 at 05:58 PM 0
Share

Hmmm... those should work the same. The only thing I can think of is maybe the length of the dynamic string is causing problems? Try making the hard-coded one really long and see if that has an effect.

avatar image dhaggerfin FortisVenaliter · Jun 08, 2016 at 07:48 PM 0
Share

Thank you, I did but it just spilled off the card. I set some more debug lines and it made me realize that Start() was being called more than once. $$anonymous$$y script was accidentally attached to a second GameObject in my project, causing the confusion.

1 Reply

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

Answer by dhaggerfin · Jun 08, 2016 at 07:43 PM

Updated by code and added more debug logs. Hopefully this can shed some light:

 using UnityEngine;
 using System.Collections;
 
 public class CardCreator : MonoBehaviour {
 
     public string prefix;
     public string suffix;
     public string creatureType;
     public int attack;
     public int defense;
     public string abilities;
     public char color;
 
     string cardName;
 
     // Use this for initialization
     void Start () {
         Debug.Log ("Card Name before Start = '" + cardName + "'");
         cardName = prefix + " " + creatureType +  " " + suffix;
 
         GameObject g = GameObject.FindGameObjectWithTag ("CardName");
         TextMesh t = g.GetComponent<TextMesh> ();
         t.text = cardName;
         Debug.Log ("Card Name after Start = '" + cardName + "'");
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log ("Card Name in Update = '" + cardName + "'");
     }
 }


Output in the console:

alt text


console.png (20.8 kB)
Comment
Add comment · Show 2 · 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 dhaggerfin · Jun 08, 2016 at 07:46 PM 0
Share

Seems like the variables set in the UI are losing their values. Also, Start is being called more than once? That doesn't seem right.

http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

avatar image dhaggerfin dhaggerfin · Jun 08, 2016 at 07:47 PM 0
Share

Yes, that was it! $$anonymous$$y script was attached to another game object as well. This was clearing the values.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why is my scene variable losing its value? 0 Answers

Why does this script require me to assign this variable twice to work? 1 Answer

Change variable independently for different objects with same script.,Change variable in different object but same script 0 Answers

How to change the player gravity level?,How to change the value of the players gravity? 1 Answer

Why is the private int variable in this c# program not updating? 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