Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 digzou · Nov 26, 2014 at 07:52 AM · javascriptinstantiateupdateguitext

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.

alt text

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

guitextissue.png (3.4 kB)
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

1 Reply

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

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.

Comment
Add comment · Show 3 · 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 digzou · Nov 26, 2014 at 08:59 AM 0
Share

Thank you :)

avatar image digzou · Nov 26, 2014 at 09:47 AM 0
Share

I just had to add "yield" in the moveUp() at line 16; :)

avatar image Kiwasi · Nov 26, 2014 at 10:12 AM 0
Share

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

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

Performance Optimization ~Function Update: Loop or Once ? 5 Answers

How can I declare class properties of an object in a single line? 2 Answers

Instantiate position from four options. 1 Answer

How to correctly convert Object to GameObject 1 Answer

Please help with this code!! 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