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 vikingnm · Jan 28, 2015 at 11:05 AM · scripting problemgui.label

Trying to access GUI.Label text

I'm trying to change the GUI.Label text in C# and it seems to be working fine; however, when attached to different objects it's almost like each object is creating a separate instance of the GUI.Label. Each click of a different object changes the text and clears it after the 5 second invoke as it should but clicking another object overlays the new text instead of clearing it...again as if it's creating a new instance. I had a version where I created the new protected Rect and set it in Start() but it was doing the same thing.

Does anyone know the C# dot reference to dynamically created GUI.Label items? or if it is creating instances of the same GUI.Label to basically clear all of them out before setting the text again? i tried searching but nothing seems to be coming up in reference to this. Would it be creating new instances of the GUI.Label or instances of the text inside?

Using 2 scripts EventManager.cs and inven.cs (where the onClick is called)

EventManager.cs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class EventManager : MonoBehaviour {
     protected string thisString = "";
     private GUIStyle myStyle = new GUIStyle();
     private RectOffset rctOff;
     private int textWidth = Screen.width - 50;
     private int textHeight = 10;
     protected int textBoxTop = 30;
 
     protected void displayMessage(string displayString, int lineCount) {
         thisString = displayString;
         textBoxTop = textHeight * lineCount;
         
         CancelInvoke();
         Invoke ("removeText", 5);
     }
     
     void removeText() {
         thisString = "";
     }
 
     void OnGUI() {
         Font thisFont;
         thisFont = Resources.Load<Font> ("Japonesa_0");
 
         myStyle.font = thisFont;
         myStyle.fontSize = 20;
         myStyle.normal.textColor = Color.white;
         myStyle.alignment = TextAnchor.MiddleCenter;
         myStyle.wordWrap = true;
         myStyle.fixedWidth = textWidth;
         myStyle.fixedHeight = textHeight;
         myStyle.name = "textBox";
 
         rctOff = GUI.skin.customStyles[0].overflow;
         myStyle.overflow = rctOff;
 
         GUI.Label (new Rect (Screen.width / 2 - textWidth / 2, textBoxTop, 100, 30), thisString, myStyle);
     }
 }
 


inven.cs using UnityEngine; using System.Collections;

 public class inven : EventManager {
 
     void OnMouseDown () {
         base.displayMessage ("You clicked " + this.name + " ~ " + Random.Range(-10.0F, 10.0F).ToString(), 1);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Using For Loop index as if statement parameter within the loop 0 Answers

Trying to make an object "Flee" from the player but it won't work? 3 Answers

"Mouse Scrollwheel" on xbox 360controller 2 Answers

error Int to string?Assets/Scripts/MyConnect.cs(61,60): error CS0029: Cannot implicitly convert type `int' to `string' 1 Answer

Please help! After build on Android UI buttons can't enable/disable script but works when using Unity remote. 0 Answers


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