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 alilograpi · Mar 24, 2013 at 11:33 PM · guipickup

Need a little help with a pick up script

Hey guys , I need you to help me with my pick up script.

using UnityEngine; using System.Collections.Generic;

public class Keyring : MonoBehaviour { public AudioClip getKeySound; public float keyPickUpDistance = 5f;

 private List _keys = new List();
  
 public void AddKey(Key key)
 {
 _keys.Add(key);
  
 if (getKeySound != null)
 {
 audio.PlayOneShot(getKeySound);
 }
 }
  
  
 public bool HasKey(Key key)
 {
 return _keys.Contains(key);
 }
  
  
 void Update()
 {
 if (Input.GetKeyDown(KeyCode.E))
 {
 RaycastHit hit;
 if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, keyPickUpDistance))
 {
 Key key = hit.transform.GetComponent();
 if (key != null)
 {
 AddKey(key);
 key.transform.parent = transform;
 key.transform.localPosition = Vector3.zero;
 key.gameObject.SetActiveRecursively(false);
 }
 }
 }
 }

}

This script is attached to my player.When I press the E button my player take the key and it work correct but I need to make a GUI appear(when I press E) and ask me if I want to pick it up or no.Can someone help me with that , because I dont actually know how does the gui on C works. Thank you for your time.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ExTheSea · Mar 25, 2013 at 12:07 AM

First, if you don't know anything about Unity GUI you should check out this: http://docs.unity3d.com/Documentation/Components/gui-Basics.html

Then what you could do is something like this:

 private boolean displayGUI = false;
 
 void Update()
 {
 if (Input.GetKeyDown(KeyCode.E))
 {
 RaycastHit hit;
 if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, keyPickUpDistance))
 {
 Key key = hit.transform.GetComponent();
 if (key != null)
 {
    displayGUI = true;
 }
 }
 }
 
 void OnGUI(){
   if(displayGUI){
     //display a window and stuff... to do that follow the link
         if(GUI.Button("Yes")){
           AddKey(key);
           key.transform.parent = transform;
           key.transform.localPosition = Vector3.zero;
           key.gameObject.SetActiveRecursively(false);
           displayGUI = false;
         }
         if(GUI.Button("No"))
           displayGUI = false;
   }
 }

This is untested code so i don't know whether or how well it will work in your case

Let me know if you run into problems.

Comment
Add comment · Show 5 · 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 alilograpi · Mar 25, 2013 at 11:25 AM 0
Share

So I got some errors but I fixed them and now the script looks like this :

using UnityEngine; using System.Collections.Generic;

public class $$anonymous$$eyring : $$anonymous$$onoBehaviour { public AudioClip get$$anonymous$$eySound; public float keyPickUpDistance = 5f;

 private List _keys = new List();
 
 public void Add$$anonymous$$ey($$anonymous$$ey key)
 {
     _keys.Add(key);
     
     if (get$$anonymous$$eySound != null)
     {
         audio.PlayOneShot(get$$anonymous$$eySound);
     }
 }
 
 
 public bool Has$$anonymous$$ey($$anonymous$$ey key)
 {
     return _keys.Contains(key);
 }
 

private bool displayGUI = false;
void Update() { if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) { displayGUI = true; } } void OnGUI(){ if(displayGUI){ //display a window and stuff... to do that follow the link if(GUI.Button(new Rect(20,40,80,20), "Yes")) { RaycastHit hit; if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, keyPickUpDistance)) { $$anonymous$$ey key = hit.transform.GetComponent(); if (key != null) { Add$$anonymous$$ey(key); key.transform.parent = transform; key.transform.localPosition = Vector3.zero; key.gameObject.SetActiveRecursively(false); displayGUI = false; } if(GUI.Button(new Rect(20,40,80,20), "No")) { displayGUI = false; } } } } } }

But It still doesnt work , It displays only 1 button (yes) and when I press it it doesnt do anything.

avatar image ExTheSea · Mar 25, 2013 at 12:13 PM 0
Share

Can you please format your bottom code again because it's really difficult to read this way. From what i can see is that when you press the E Button a window pops up and when you press Yes a raycast gets send out. This would in my opinion be a bit weird as the window will appear even if you have nothing in front of you. I would make it so that the raycast gets send if you press e and if it hits a key it sets the boolean to true and maybe saves the key in a variable. Then the window should open and if you press yes the key gets added. The No Button is propable behind the Yes Button. Try changing the Coordinates of the No Button.

avatar image alilograpi · Mar 25, 2013 at 12:53 PM 0
Share

So the promlem was that the OnGUI and the Update fuctions were spliting the script. I fixed that by using a new var (add1) and now my script look like this :

using UnityEngine; using System.Collections.Generic;

public class $$anonymous$$eyring : $$anonymous$$onoBehaviour { public AudioClip get$$anonymous$$eySound; public float keyPickUpDistance = 5f;

 private List _keys = new List();
 
 public void Add$$anonymous$$ey($$anonymous$$ey key)
 {
     _keys.Add(key);
     
     if (get$$anonymous$$eySound != null)
     {
         audio.PlayOneShot(get$$anonymous$$eySound);
     }
 }
 
 
 public bool Has$$anonymous$$ey($$anonymous$$ey key)
 {
     return _keys.Contains(key);
 }
 
 private bool displayGUI = false;
 private bool add1 = false;

 void Update()
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
     {
         RaycastHit hit;
         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, keyPickUpDistance)) 
         {
             $$anonymous$$ey key = hit.transform.GetComponent();
             if (key != null)
             {
                 displayGUI = true;
                 if (add1 = true) {
                 Add$$anonymous$$ey(key);
                 key.transform.parent = transform;
                 key.transform.localPosition = Vector3.zero;
                 key.gameObject.SetActiveRecursively(false);
             }
             }
         }
     }
 }
 

void OnGUI () { if(displayGUI){

     GUI.Box(new Rect(10,10,100,90), "Pick Up ?");

add1 = false;

     if(GUI.Button(new Rect(20,40,80,20), "Yes")) {
         add1 = true;
         displayGUI = false;
     }

     
     if(GUI.Button(new Rect(20,70,80,20), "No")) {
         add1 = false;
         displayGUI = false;
     }
 }

} }

It work and it display gui when I press the E button next to the key but it pick up the key in any case never$$anonymous$$d if the I press the yes button or no and even if I dont press anything.

avatar image ExTheSea · Mar 25, 2013 at 01:28 PM 0
Share

I don't know why you're using the add1 boolean. In your current state the script works like this. If you pressed Yes the first time the add1 variable gets set to true and because of that it will pick up the key but the problem is it doesn't gets set back to false. so next time you press e on a key the window will open but the key will picked up even if you don't press anything. Either you set add1 to false after you picked up the key or you do it like this:

   using UnityEngine; using System.Collections.Generic;
 
 public class $$anonymous$$eyring : $$anonymous$$onoBehaviour { public AudioClip get$$anonymous$$eySound; public float keyPickUpDistance = 5f;
 
     private List _keys = new List();
     prvate $$anonymous$$ey keysave;
      
     public void Add$$anonymous$$ey($$anonymous$$ey key)
     {
     _keys.Add(key);
      
     if (get$$anonymous$$eySound != null)
     {
     audio.PlayOneShot(get$$anonymous$$eySound);
     }
     }
      
      
     public bool Has$$anonymous$$ey($$anonymous$$ey key)
     {
     return _keys.Contains(key);
     }
      
     private bool displayGUI = false;
     private bool add1 = false;
      
     void Update()
     {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
     {
     RaycastHit hit;
     if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, keyPickUpDistance))
     {
     $$anonymous$$ey key = hit.transform.GetComponent();
     if (key != null)
     {
     keysave = key;
     displayGUI = true;
     }
     }
     }
     }
 
 void OnGUI () { if(displayGUI){
 
     GUI.Box(new Rect(10,10,100,90), "Pick Up ?");
 
 add1 = false;
 
     if(GUI.Button(new Rect(20,40,80,20), "Yes")) {
     Add$$anonymous$$ey(keysave);
     keysave.transform.parent = transform;
     keysave.transform.localPosition = Vector3.zero;
     keysave.gameObject.SetActiveRecursively(false);
     displayGUI = false;
     }
      
      
     if(GUI.Button(new Rect(20,70,80,20), "No")) {
     displayGUI = false;
     }
     }
 
 } }

In my opinion this is cleaner but your way should work too i think.

avatar image alilograpi · Mar 25, 2013 at 01:55 PM 0
Share

Ok , thanks a lot for your help , I'll keep working on it.

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

Multiple Cars not working 1 Answer

How to make a "Pick Up" script? 2 Answers

Show GUI.Label when touching trigger 4 Answers

Dialouge text. 1 Answer

Can't get Gui Pos on top of Enemy ingame! 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