- Home /
PickUp (Destroy) gameObject one by one
Hi. I have 2 gameObject, let say the first one is phone and the second one is lighter. and also, i input a button 'T' to take. first, i want to take the phone, and then the lighter.. my issue here is both phone and lighter are gone when i press 'T' button. I want to take them one by one, not both of them! this is my code
private var DrawGUI = false;
function Update()
{
if(Input.GetKeyDown(KeyCode.T))
{
itemCollected();
}
}
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Player")
{
DrawGUI = true;
}
}
function OnGUI ()
{
if(DrawGUI == true)
{
GUI.Label(new Rect(Screen.width*0.5-50, 200, 100, 100), "Take (T)");
}
}
function itemCollected()
{
Destroy(gameObject);
}
can't I use a script for it? or i must separate the script each objects? thx
Answer by Gruffy · Dec 19, 2013 at 04:00 PM
hey bud, here you go.
Start with this and we can move forward link text
Hope this helps you out dude.
gruffy..
Just to inform you, it is a full scene with two cube in there, both tagged seperately with phone and lighter, the logic comes form the CollectStuff script found attached to the character controller. Cheers bud:)
@Gruffy wow, it works like a charm!! really appreciate what u did.. :) i realize some of my mistake, since i saw your explanation on there.. thank u so much man :D
Answer by Patrykgazing · Dec 16, 2013 at 04:01 PM
private var DrawGUI = false;
private var IsPlayerPickingItem = false;
function Update()
{
if(Input.GetKeyDown(KeyCode.T))
{
if(!IsPlayerPickingItem){
IsPlayerPickingItem = true;
itemCollected();
IsPlayerPickingItem = false;
}
}
}
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Player")
{
DrawGUI = true;
}
}
function OnGUI ()
{
if(DrawGUI == true)
{
GUI.Label(new Rect(Screen.width*0.5-50, 200, 100, 100), "Take (T)");
}
}
function itemCollected()
{
Destroy(gameObject);
}
Answer by emalb · Dec 16, 2013 at 04:04 PM
You could put a public variable in there that represents the number of presses required to collect your item, say numPresses. Then, in your Update function, each time you detect a key has been pressed you could decrease the numPresses variable and only call itemCollected when it reaches zero.
If you set the numPresses to 1 for your phone and 2 for your lighter (in the Inspector) you should be able to get this to do what you need.
anyway i tried your solution with public variable, and do like what you told me above, i can take the phone that set to 1 but i cannot take the lighter that set to 2..
but since i am a beginner on javascript and unity, maybe i did wrong.. :/
can u write the script here? thanks anyway for the solution and sorry if i am inappropriate :/
I'm happy to help, but I would like to see your latest script for this. It saves me actually doing the work, but it also makes it easier for myself and others to see your thought processes and help you find your own way around it.
i just modified some from this script.. but both phone & lighter still gone, and also there is no error.. :/
#pragma strict
var numPresses : int ;
private var DrawGUI = false;
function Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T) && numPresses == 1)
GameObject.Destroy(gameObject.Find("Phone"));
else if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T) && numPresses == 2)
GameObject.DestroyObject(gameObject.Find("lighter"));
}
function OnTriggerEnter (info : Collider)
{
if(info.tag == "Player")
{
DrawGUI = true;
}
}
function OnGUI ()
{
if(DrawGUI == true)
{
GUI.Label(new Rect(Screen.width*0.5-50, 200, 100, 100), "Take (T)");
}
}
I would put a trigger on each of your items to pick up Then i might use the tag dropdown in the inspcetor for each item and give them a meaningful name (like lighter, phone, etc)
On the player i would then use its own collider (whichever u have on it to check for trigger collisions)
The simplest implementation would be to conditionally check like so....
function OnTriggerEnter(col: Collider)
{
if(col.gameObject.tag == "phone")
{
Destroy(this);
//or col.gameObject.SetActive(false);
}
else if(col.gameObject.tag == "lighter")
{
Destroy(this);
//or col.gameObject.SetActive(false);
}
else
{
return;
}
}
}
lol, totally untested in Unity, but you hopefully get the idea.
Dont just copy it in there, have a gander at what its doing 1st my man.
Thanks for reading. Gruffy PS- comment back after youve had a figure out of it for a second, if its still not happening, I will write for you, im just a bit busy atm...anyhoo, let me know bud :)
lol, thanks for that man.. i read n understanding the code, and also tried yours, but its still not happening.. :(
i did tag each objects for the trigger too, but still nothing happened ..
anyway FYI this one for FPS game, survival horror stuff :p