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 /
avatar image
0
Question by hettex · Aug 26, 2011 at 12:50 AM · guierrortextoncollisionenter

GUI Text Problem

Hello,

I have been having some issues with my GUI text. As you can see from the script i would like the Gui text to update with the counter. var Counter : int = 0; private var lastCube : GameObject; function OnCollisionEnter (myCollision : Collision) { if(myCollision.gameObject.name == "Cube" && myCollision.gameObject != lastCube){ Counter++; guiText.text = "Score : " +Counter; lastCube=myCollision.gameObject; } }

I have attached this script to the projectile that will hit the cube, i receive no console errors and i have attached the textgui to the projectile. Any help is much appreciated.

Comment
Add comment · Show 3
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 Illogical-Ironic · Aug 26, 2011 at 01:14 AM 0
Share

Just to be sure, which language are you using? Javascript? C#?

avatar image hettex · Aug 26, 2011 at 01:24 AM 0
Share

Javascript

avatar image Illogical-Ironic · Aug 26, 2011 at 06:13 PM 0
Share

I think you need to use the OnGUI function to show text on the GUI.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Molix · Aug 26, 2011 at 01:59 AM

The issue is that the GUIText Component uses a special normalized coordinate system, meaning for it to show on the screen its x and y coordinates need to be between 0 and 1 (e.g. 0.5, 0.5 is the middle of the screen). Since you have attached it to the projectile, that is unlikely. Instead, put the GUIText in a separate GameObject that doesn't move. You will have to get a reference to that new object so you can tell the score to change, but there are several approaches to that (e.g. keep a reference, Find(), etc).

Comment
Add comment · 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
0

Answer by aldonaletto · Aug 26, 2011 at 02:22 AM

guiText is meaningful only if the object is a GUIText. Assigning the GUIText to the projectile makes it a child, what doesn't set the projectile's variable guiText.
Another problem: the variable lastCube exists in each projectile you instantiate; if you create a new projectile each time you shoot, lastCube will always start with the null value.
A simple way to make this work is to create a script for the GUIText (let's call it GuiScore.js), place the variables lastCube and Counter in it and declare them as static, so they can easily be accessed from the projectile script:

static var lastCube: GameObject; static var Counter: int = 0;

function Update(){ guiText.text = "Score : " +Counter; } Then change the projectile script to:

function OnCollisionEnter (myCollision : Collision) {
    if (myCollision.gameObject.name == "Cube" && myCollision.gameObject != GuiScore.lastCube){
        GuiScore.Counter++;
        GuiScore.lastCube = myCollision.gameObject;
    }
}
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI Text Blinking came up with one error. 3 Answers

Change text gui.button in a for bucle 1 Answer

Displaying a collision with Obstacle with text on screen? 0 Answers

converting from GUI text to GUI Labels 0 Answers

Instantiating object from inventory 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