- Home /
Help with OnTriggerEnter and mouse down
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 = "";
}
}
First things first: is the Is Trigger option activated on the collider of the Player?
Answer by SterlingStudios · May 18, 2014 at 08:33 PM
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 = "";
}
}
Answer by robertbu · May 18, 2014 at 02:40 AM
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().
This didn't work, unless I did it wrong. Did all I need to do is just change the function name and the one line of code?
Your answer