- Home /
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
If you're asking if that's possible to do with a script, then yes. If you're asking if there's a script floating around on the Net that does specifically that, then I can only say it's possible that there might be. That is such a trivial task that there might be a plug-and-play script designed to do specifically that somewhere in the great vastness of the Internet, but I am not omniscient so I cannot claim certainty of that as fact.
EDIT: Typed omnipotent when I meant omniscient. Durrr... XD
what do you mean ive watched a lot of tutorials but i havent found any that cover this?
Break your feature down into its basic parts. You want to:
Press a key to interact with the world
Have that interaction trigger an animation of a button in your world (?)
Play a sound
There are more than a few tutorials on this kind of thing as well as older questions covering everything above. Additionally, if you look through the documentation for unity, it's all covered there, too.
can you give me a link to a tutorial im still having trouble finding one :(
Answer by deeredman1991 · Jul 10, 2011 at 11:15 PM
its Collision collision don't forget the capitol and lowercase :)
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 :)
(I had this as a comment but I re posted as an answer so you could better read my code :)
ok so now i need to know how to add the song into that code is there something i need to keep do i keep it in () or what?
Answer by deeredman1991 · Jul 10, 2011 at 08:28 PM
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 :)
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
http://answers.unity3d.com/questions/20233/how-do-i-play-a-sound-during-a-level-change.html
thanks a lot this will get the game finished in 3-6 weeks
no prob bro...I added a website to my post to $$anonymous$$ch you how to play a sound file :)
ok i just inputed the script but it has an error: assets\button action script.cs(1,2)error:cs8025: parsing error
normally when I get a parsing error it's because I forgot a ;...or is it a {...lol I forget
you might have to write this in your function (not sure)
Collision collision
...here is my script that I use the tag finding thing for
IEnumerator OnCollisionEnter(Collision collision)
{
//with bandit
if (Time.time>nextDmg && collision.gameObject.tag == "Bandit")
{
nextDmg = Time.time + noDmg;
//if bandit is to the right
if (Physics.Raycast(transform.position,Vector3.right,10))
{
//take controls away from the player
isstuned -= 1;
etc...etc...
alright ive added the animation for when you press the button all i have to do is add the easter egg song and action script if i can get it to work
Your answer
Follow this Question
Related Questions
Trigger sound when pressing button 2 Answers
Resume Button Sound Not Playing 1 Answer
GUI Button sound problem, don´t work 1 Answer
Triggering random sound on player 1 Answer
Totally new noob to mecanim and unity itself - Trigger animation? 0 Answers