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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Tomperke · Dec 06, 2012 at 03:24 PM · guioncollisionentertextbox

Simple text in a box - on player collision

As a designer more then a programmer im currently lost in the mist. I know how triggering works but dont seem to get how this one should work :

1) SITUATION: A player (tagname) collides with an object. ----> i already know how to make a simple check if or not player touches object...

2) Object has a collision script on it showing a text. ??????? How do i tell the collision script to look for a tekst?

3) Object shows a tekst in a box middle of the screen. ??????? How to position the text?

3) Box & text go away when user press enter. ??????? How to tell unity when a button is entered?

  • It would be great to have a variable tekst that van be changed in inspector. This will allow me to recycle the same script every time i need to show a tekst.

Im really worthless in scripting and trying to avoid it since this is a easy "myst like" game. But now I need a full script in C or java. Normally i figure these things out myself, but this one is cracking my head for days now.

Anyone that would be willing to help me out with a full functional script would have my sincere thanks !!!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by konashadow · Dec 06, 2012 at 03:40 PM

Once you check the collision with onCollisionEnter or whatever you want, you can toggle some boolean variable (lets use "collide") to true.

Then you can use a GUI label to make the text appear.

if(collide) {

 GUI.Label(new Rect(10,10,50,50), "Collide!");
 if(Input.GetMouseButtonDown(KeyCode.Return)) {
     collide = false;
 }    

}

This code will make a text saying Collide! when collide is set to true. You can edit the position by editting the first two numbers in the 4 number sequence. The last two edit the size. The GetButtonDown will sense when you hit enter, and will set collide to false, making the text disappear. You can put this code in the update function. Also, you could declare a public string variable and replace the "Collide!" with it so you can edit the message of every collision. Hopes this helps!

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 Xerelin · Dec 06, 2012 at 03:45 PM

Inside your collision event you need to set the collider you hit as a parameter, then inside the event call up the value of the public variable housing your text.

Next, store this text in a string value at the class level and switch a bool from false to true for your GUI.

Lastly, in your OnGUI() method, you need to check to see if the bool is set to true and if it is, draw a text area on the screen. Sample code is below:

 float halfwidth = 0f;
 float halfheight = 0f;
 
 bool isCollision = false;
 string textToShow = "";
 
 void OnCollisionEnter(Collider hit)
 {
     textToShow = hit.textToDisplay;
     isCollision = true;
 }
 
 void OnGUI()
 {
     if(isCollision){GUI.TextArea(new Rect((Screen.width / 2) - halfwidth, (Screen.height / 2) - halfheight, (halfwidth * 2), (halfheight * 2)), textToShow);
 }
 
 void Update()
 {
     if(Input.GetKey(KeyCode.Enter)){isCollision = false; }
 }

Inside the script on your object being collided with just store a string value with what you want to display at the class level.

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

11 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

Related Questions

What is frame, How OnGuI is called every frame? 2 Answers

'MouseLook.sensitivityX' is inaccessible due to its protection level. 2 Answers

Trying to Play my Particle System OnCollisionEnter Script 1 Answer

Transitions When Pausing? 1 Answer

Gui Button Turn Off Object on Click 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