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 potu1304 · Oct 19, 2013 at 12:05 PM · c#gui

Add a GUIBox if Key picked up

Hello! I have found a nice script for picking up keys, but now I wonder if there is a way to add a blue texture or icon in the upper richght corner if I pick up the blue key for example. Here is the script: using UnityEngine; using System.Collections;

 public class Key : MonoBehaviour 
 {
     
     public enum KeyType
     {
         Blue,
         Green,
         Yellow,
         None
     } 
     
     public KeyType type;
     public AudioClip audioClip;
     
     void OnTriggerEnter(Collider other) 
     {
         if (other.tag == "Player")    
         {
             Inventory inventory = other.transform.GetComponent<Inventory>();
             inventory.keys.Add(type);
             
             Destroy(gameObject);    
         }
     }
     
 }


And here is the inventory script: using UnityEngine; using System.Collections; using System.Collections.Generic;

 public class Inventory : MonoBehaviour 
 {
     public List<Key.KeyType> keys;
     
     
 }


Has anybody an idea if it is possible to get this working?

Best regards

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 samtperrin · Oct 19, 2013 at 01:57 PM

You could instantiate a GameObject that is a GUITexture or has a GUI script attached. When you want to stop displaying that blue texture icon just destroy the 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
avatar image
0

Answer by RyanZimmerman87 · Oct 19, 2013 at 10:16 PM

Sounds like a basic how do I use GUI question? You could also use other methods but for something like this I always use OnGUI.

Code Example:

 //I don't know your game logic/assets so all these are just basic examples
 
 //easily format and set up all the GUI stuff with this.
 public GUIStyle keyStyle;
 
 public Texture2D keyIcon1Texture;
 public Texture2D keyIcon2Texture;
 //etc..
 
 //matrix to easily work with onGUI for all screen sizes
 vector3 matrixVector;
 
 //position and size for key icon
 vector2 keyIconPositionVector = new Vector2 (1180, 0);
 vector2 keyIconSizeVector = new Vector2(100, 100);
 
 
 //you can use a different method to set GUI for the screen size
 //this method allows automatic formatting to any screen size
 //useful for mobile devices
 float native_width = 1280;
 float native_height = 720;
 
 float rx;
 float ry;
 
 int keyInt;
 
 void Start()
 {
 
 float rx = Screen.width/native_width;
 float ry = Screen.height / native_height;
         
 matrixVector = new Vector3 (rx, ry, 1);
     
 //sets the size of key icon texture to scale with matrix    
 keyIconSize.x = keyIconSize.x * rx;
 keyIconSize.y = keyIconSize.y * ry;
 
 keyInt = 0;
 
 //or if you want to save keys 
 keyInt = PlayerPrefs.GetInt("Key Int", 0);
 }
 
 void OnGUI()
 {
 if (keyInt == 0)
 {
 return;
 }
 
 else if (keyInt != 0)
 {
 GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, matrixVector); 
 
 //blue key
 if (keyInt == 1)
 {
 //add blue texture icon to top right
 GUI.Label (new Rect(keyIconPositionVector.x, keyIconPositionVector.y, keyIconSizeVector.x, keyIconSizeVector.y), keyIcon1Texture, keyStyle);
 }
 
 //red key
 else if (keyInt == 2)
 {
 //add red texture icon to top right
 GUI.Label (new Rect(keyIconPositionVector.x, keyIconPositionVector.y, keyIconSizeVector.x, keyIconSizeVector.y), keyIcon2Texture, keyStyle);
 }
 
 //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

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

16 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

Related Questions

how can i get GUI.Button presse(or down not click) 3 Answers

Where do I assign GUI scripts? 1 Answer

Intro GUI Text Script... 3 Answers

GUI Resolution Ajust 1 Answer

Why does my GUI health bar move sideways when it I lose health? 1 Answer


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