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 evilpikmin · Sep 24, 2013 at 12:56 PM · javascriptguitrigger

How do I use a trigger to change the GUIText

Hi all, Im pretty new to Unity and Java scripting but I was making good progress until I hit this problem. Here is my code:

 function OnTriggerEnter (myTrigger : Collider) 
 {
     if(myTrigger.gameObject.name == "box")
     {
         Debug.Log("Box went through!");
         var targetText = GameObject.FindWithTag("Score");
         targetText.guiText.text = name;
     }
 }

My Debug text works, but I know I am doing something stupid with the GUI part. Can anyone help me out and point me in the right direction?

Cheers!

Comment
Add comment · Show 2
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 robhuhn · Sep 24, 2013 at 12:58 PM 0
Share

please format your code next time

avatar image evilpikmin · Sep 24, 2013 at 01:04 PM 0
Share

oops, sorry. Newbie FAIL.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tanshaydar · Sep 24, 2013 at 01:11 PM

Make sure your GameObject.FindWithTag("Score"); finds the GUItext, otherwise it will return null and won't set anything. you can check it with if(varText == null)

Alternatively, you can set your var to be a GUIText so that you can set it with targetText.text = name;

In the least, you can make it public and attach the GUIText object from hierarchy window to your script. But in order to do that, you should make your variable public.

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 evilpikmin · Sep 24, 2013 at 06:24 PM 0
Share

Alternatively, you can set your var to be a GUIText so that you can set it with targetText.text = name;

Thanks Tanshaydar, I don't think my scripting is good enough to work that out though, could you give me some more clues please!?

avatar image Tanshaydar evilpikmin · Sep 24, 2013 at 10:41 PM 0
Share

If you declare your variable as public var targetText : GUIText; you can see it in inspector view and assign your gameobject to it by dragging it to the area.

avatar image evilpikmin evilpikmin · Sep 25, 2013 at 11:03 AM 0
Share

Thanks again. I think I need more help than I thought. Should I be using 'GetComponent' to do this? I am getting more confused...

avatar image
0

Answer by fafase · Sep 25, 2013 at 11:07 AM

GUIText is a component attached to game object so once you have the object you still to fetch the component with GetComponent();

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Showing text near object 1 Answer

Setting Scroll View Width GUILayout 1 Answer

How to see minutes in the timer's box? 1 Answer

Basketball Score counter 1 Answer

4.6 Trigger.js Wrong command is running. Help? 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