- Home /
 
Text Prefrab problem
So i have prefrab with text mesh and script that changes its text and a script that is spawning it,but it is changing prefrab instead, not the gameobject. How can i change gameobject?Thanks!
Text script:
 public class Text : MonoBehaviour {
     
     public float min;
     public float speed;
     TextMesh text;
     public string score;
     // Use this for initialization
     void Start () 
     {
         text = (TextMesh)gameObject.GetComponent(typeof(TextMesh));
         text.text = score;
     }
     
 }
 Spawning script:
 public class SimpleEnemy : MonoBehaviour {
     
     
     public Transform Text;
     public float scoreDeath;
     // Use this for initialization
     void Start () 
     {
         
          Text.GetComponent<Text>().score = scoreDeath.ToString();
          Instantiate(Text,transform.position,Quaternion.identity);    
     }
 }
 
 
              Answer by mattssonon · Aug 20, 2013 at 04:39 PM
Instantiate returns the cloned object on which you should make your changes, you're just directly changing the prefab.
Try this in the spawn script:
 Transform clone = (Transform)Instantiate(Text,transform.position,Quaternion.identity);
 clone.GetComponent<Text>().score = scoreDeath.ToString();
 
              Thank it works! But i think this line should be Transform clone = (Transform)Instantiate(Text,transform.position,Quaternion.identity);
Ah, yes, you're right, I will edit my answer accordingly. Thanks.
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why is this room always spawning on left side? 1 Answer
Rhythm Game Out of Sync (C#) 1 Answer