- Home /
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.
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.
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.
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.
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.
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.
Ok , thanks a lot for your help , I'll keep working on it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095023im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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