button action script

is there a script that adds action to an object like walk up to a button and press a key to press the button? If so what is the script?

I need this because on my game i am trying to add an Easter egg that when you press a red button that is on the floor the song tragedy by celldweller will start to play

I’m not sure how to play the sound but your script would go something like this in c#

you said it was on the floor so this should handle it :slight_smile:

if(Input.GetKey("space") && collision.gameObject.tag == "redbutton")
{
//(insert play sound file here)
}

oh and you need to tag the button with the tag “redbutton”

if this doesn’t work you can always use a raycast…although how you get a raycast to tell if there is a specific object under it…i have no idea lol

maybe this page can teach you about playing sounds

its Collision collision don’t forget the capitol and lowercase :slight_smile:

you insert it in the () of your function…

(I’m not sure if this will work) like for example

void Update(Collision collision) 
{ 
if(Input.GetKey("space") && collision.gameObject.tag == "redbutton") 
{ 
//(insert play sound file here) 
} 
}

but you could always just create a new function like I did and call it in you update function like this

void Update() 
{ 
//call the collision function
OnCollisionEnter() 
}

//collision function
void OnCollisionEnter(Collision collision) 
{ 
if(Input.GetKey("space") && collision.gameObject.tag == "redbutton") 
{
//insert play sound here
}
}

(actually I dont even think you have to call the OnCollisionEnter function I think it’s build in automatically you just have to create it in your script to add anything too it)

hope this helps :slight_smile:

(I had this as a comment but I re posted as an answer so you could better read my code :slight_smile: