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 /
  • Help Room /
avatar image
0
Question by loxagos_snake · Sep 21, 2016 at 03:52 PM · interactiongui text

Displaying an item's description on screen using the GUI (Resident Evil style)

Hello! This is a follow up to my previous question about setting up the interactions (http://answers.unity3d.com/questions/1245359/cant-get-simple-interaction-code-resident-evil-sty.html#comment-1245390), which I solved using raycasts to detect the object and output its description on the console for testing purposes. Now, I want to integrate the GUI system to display a text on the lower center part of the screen. In case you don't have time to read the previous question, here's what I want to achieve: walk up to the object, press the interaction key, pause the game, display the item's description as a GUI text and press Interact again to return the flow of the game and erase the description. The problem is that I don't know how to do that since I have to use OnGUI (you'll see what I'm talking about below, I'll provide the relevant code with explanation for simplicity's sake)

There are three scripts that are relevant to this task: PlayerController (controls the player and checks for interactions), InteractiveProp (added on the items, only contains the public string itemDescription so far) and GameManager which, for the time being, I use to store the definitions of the functions for convenience. In my PlayerController script, I have the following code:

 void Update()
     {
         // Detect various interactions
         if (Input.GetButtonDown ("Interact"))
         {
             gmHandle.InteractWithProp();
         }
     }

where gmHandle is an object of type GameManager that I use to access the script and subsequently, it's InteractWithProp() function. Next is my GameManager script:

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
 
     // ---------- THIS SCRIPT IS USED TO MANAGE THE GAME AND STORE FUNCTIONS TEMPORARILY OR PERMANENTLY --------- //
 
     public PlayerController playerHandle;
     public GameObject playerGameObject;
 
     public float textX,textY;
     
 
     void Start () 
     {
         playerGameObject = GameObject.FindGameObjectWithTag("Player");
         playerHandle = playerGameObject.GetComponent<PlayerController>();
     
     }
     
 
     void Update () 
     {
     
     }
 
     void OnGUI()
     {
         if (playerHandle.isInteracting)
         {
 
             Rect test = new Rect(textX,textY,Screen.width/4,Screen.height/4);
             GUI.Box (test,"This is a test text!");
         }
     }
 
 
     // --------DEVELOPER DEFINED FUNCTIONS-------- //
 
     // Tests if there's an interactive object and activates its properties
     public bool InteractWithProp()
     {
         if (!playerHandle.isInteracting)
         {
             Debug.Log ("Player Pressed E");
             // Cast a ray from the player forward to detect object (DO: IMPLEMENT LAYER MASK FOR OPTIMIZATION)
             RaycastHit hitInfo;
             Vector3 rayCorrection = new Vector3(0f,0.5f,0f); // Raises the casted ray above ground
             if (Physics.Raycast ((playerGameObject.transform.position) + rayCorrection ,playerGameObject.transform.forward,out hitInfo,2f))
             {
                 if (hitInfo.collider.tag == "InteractiveObject")
                 {
                     Debug.Log ("Succesful Interaction!");
 
                     // Pause the game
                     Time.timeScale = 0;
                     
                     // Get the item description
                     string propDesc = hitInfo.collider.gameObject.GetComponent<InteractiveProp>().itemDescription; 
                     
                     Debug.Log (propDesc);
                 }
                 
                 playerHandle.isInteracting = true;
             }
         }
         
         else 
         {
             Time.timeScale = 1f;
             Debug.ClearDeveloperConsole();
             playerHandle.isInteracting = false;
         }
     }
 }
 

What you see in here regarding the GUI is only for test purposes so far. As I said, what I want to do is access the itemDescription string of the object that was detected and output that as text on the screen. But I'm not quite familiar with OnGUI() and I don't know how to access that variable, since it is only accessed inside InteractWithProp() if and only if there's a ray hit. I could possibly include another call to the function in OnGUI() but I don't think it's efficient.

Do you have any ideas on how I could solve that? Any other suggestions will be much appreciated!

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

69 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 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 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 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 avatar image

Related Questions

How to make pie menu options interact to OnMouseDown? 0 Answers

webgl app is not working in browser 0 Answers

How do you get different parts of the same object to move in different ways? 1 Answer

My collisions won't work whenever I pick it up with the XR Toolkit 0 Answers

Unity 3D #C Nightvision does not want to switch on or off. 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