Hello. I made a script, which in theory should work, but doesn’t. I want it to when I click, while in the collider, destroy the prop and display the phone. It’s kind of like a fake pick-up system. Please help!
PickupPhone.js:
#pragma strict
var guitext : GUIText;
var phone : GameObject;
function OnTriggerEnter (other : Collider){
if(other.gameObject.tag == "Player") {
guitext.text = "click to pick up the phone";
{
if(Input.GetMouseButtonDown(0)){
phone.active = true;
Destroy(gameObject);
}
}
}
}
function OnTriggerExit (other : Collider){
if(other.gameObject.tag == "Player") {
guitext.text = "";
}
}
The problem is that OnTrigerEnter() and GetMouseButtonDown() are both only true for a single frame. So unless you are extremely lucky, nothing will happen. One fix is to change OnTriggerEnter() to OnTriggerStay() and to change GetMouseButtonDown() for GetMouseButton().
Okay, I got it. I don’t know what I did, just kept re-writing it until I got it. Here’s the code:
#pragma strict
var guitext : GUIText;
var phone : GameObject;
function OnTriggerStay (other : Collider){
if(other.gameObject.tag == "Player") {
guitext.text = "click to pick up the phone";
{
if(Input.GetMouseButton(0)){
phone.active = true;;
//phone.active = true;
//arm.renderer.enabled = true;
//thelight.enabled = true;
guitext.text = "";
Destroy(gameObject);
}
}
}
}
function OnTriggerExit (other : Collider){
if(other.gameObject.tag == "Player") {
guitext.text = "";
}
}