Why isn't this input code working?

]#pragma strict


function OnTriggerEnter (myTrigger : Collider) {
 if(myTrigger.gameObject.name == "First Person Controller"){
 if(Input.GetMouseButton(0)){
        Debug.Log("Pressed left click.");
}
}
}

I put it on a box collider, set it to is trigger, and its not working, if I take out the input,getmousebutton it will, ive tried variations on this too, any help would be great, thanks.

You are asking to, in one frame, one gazillionth of a second, to not only check the trigger enter, but also the mouse input. A user will no way ever click the exact same frame the object enters the trigger. Well he might but only one in three bazillion tries.

so how do I fix this?

What are you trying to do?

here:

#pragma strict

function OnTriggerEnter (myTrigger : Collider) {
if(myTrigger.gameObject.name == “First Person Controller”){
audio.Play(44100);
}
}

This, but have it play when the user clicks the left mouse as well as be within the collider, This works, but without the clicking left mouse part

Is this all the code you have?

You are not using an update somewhere else?

for this specific object, yes thats all the code I have… I’m just unsure of what to have to take input from the mouse

So, if you want input from the mouse to occur only when object is in trigger, then use onTriggerStay. If not, and you want mouse input always, use Update.

This isn’t working,

#pragma strict

function OnTriggerStay (myTrigger : Collider) {
if(myTrigger.gameObject.name == “First Person Controller”){
if(Input.GetMouseButtonDown(0)){
audio.Play(44100);
}
}
}

It plays regardless if I’m in or out of the collider, can you give me some corrections?

Not sure. All I can see is that, I use “other”, not myTrigger.

All I need is something that will play audio when the mouse is clicked and the player is in the collider, I know people generally don’t do this here, but could you write me a script to do this? Thanks, I turned off play on awake, and it doesn’t work at all now…

So you need to meet two conditions. A. The object must enter trigger. ( does it have to be in the trigger when clicked?) please answer.

ok, it should in the trigger when clicked, otherwise it shouldn’t register the mouse. Really, The audio shouldn’t play unless it is within the trigger’s area, and being clicked. Thanks

Edit. I just noticed, you are calling you audio.Play() incorrectly, I believe. What I do is this…

var thisClip : AudioClip;
audio.clip = thisClip;
audio.Play();

Try that.

doesn’t seem to work

Really? Please just it my above code in at start() to see if sound is coming and then try your method. Let me know if both play.

still doesn’t seem to work… I put in the code several different ways, but still doesn’t work. Maybe I’m implementing it wrong, where should I put it in this code? Can you just copy and paste this with your added code?

Ok, what you need to learn is print();

Do this…

function OnTriggerStay(other : Collider)
{
if (other.tag == "thisTagNameEtc")
{
print("other has entered and is staying");
if(Input.GetMouseButtonDown(0))
{
print("receiving mouse input");
audio.Play();
}
}
}

Alright looks good, but two errors on line 26: Unknown Identifier IF, and GetMouseDown is not a member of unity engine.input

EDIT Nevermind I fixed these minor errors, just caps and GetMouseButtonDown. But For some reason it still doesn’t work!!

Ok, so, does your trigger have an audio source component? Are all the prints coming out? Are you assigning the audio.clip?