- Home /
 
Update call for Multiple GUIText fails
I need to create a GUIText everytime a player makes a correct move. Once the GUIText is created I need to move it to the top of the screen (Some what like bubbles). I have been able to create and move the GUIText. But whenever a new GUIText is created the previous ones stop updating and the GUIText doesnt move. I have been struggling to get it working for the past couple of days.
I am calling the initGuiText() inside OnCollisionEnter2d of another gameobject's script.
Here is a screenshot of how it appears.

Is there a better way to implement what I am trying to achieve?
Here is my code :
 #pragma strict
 public class invokePlusOne extends MonoBehaviour{
 var guiTextPrefab : GameObject;
 var newGui : GameObject;
 
 function initGuiText(colObj : Color32, pos :Vector3){    
     newGui = GameObject.Instantiate(guiTextPrefab,Vector3(0,0,0),Quaternion.identity);
     newGui.name="guiText"+i;
     newGui.guiText.text = "+1";
     newGui.guiText.color=colObj;
     newGui.guiText.fontSize=Screen.height/20;
              //I do this to create GUIText at 4 different positions.
          if(pos.x > 0){newGui.guiText.transform.position = Vector3(0.85,0.5,0.0); }
          if(pos.x < 0){newGui.guiText.transform.position = Vector3(0.15,0.5,0.0); }
          if(pos.y > 0){newGui.guiText.transform.position = Vector3(0.5,0.75,0.0); }
          if(pos.y < 0){newGui.guiText.transform.position = Vector3(0.5,0.25,0.0); }
 }
 
 function Update(){
     if(newGui!=null){
     moveUp();
      }
 }
 
 function moveUp(){
      while(newGui.guiText.pixelOffset.y < 1000){
          newGui.guiText.pixelOffset.y += 0.5*(Time.deltaTime);
          yield;
      }
  }
 }
 
               Thanks in advance. :)
Answer by Kiwasi · Nov 26, 2014 at 08:02 AM
You need to store a list of all of your created GUITexts and then use a loop to move them all up. I can show you the code in C#.
 List<GameObject> newGUIElements = new List<GameObject>();
 
 void initGuiText(Color32 colObj, Vector3 pos){
     // Do your own code here as written
     newGUIElements.Add(newGUI);
 }
 
  void Update(){
      foreach (GameObject oldGUI in newGUIElements){
          moveUp(oldGUI);
      }
  }
  
  void moveUp(GameObject oldGUI){
     oldGui.guiText.pixelOffset.y += 0.5*(Time.deltaTime);
  }
 
               Note you could make this more efficient by storing GUITexts in the list instead of GameObjects
This is not the way I would structure it. I would add a component to each GUIText that moves it up.
Thought you would figure that out. Your coroutine structure looked a little odd to me, so I left it alone. But if it works then go for it.
Your answer