Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Supercrusher9000 · Jul 10, 2013 at 06:40 PM · collisionaudio

Walking sound changing on surface

Hey Unity users, I wanted to redo my question since last time. I kinda forgot to include the question in that one, sorry it was my first post. But anyway I am making a horror game and want the walking sound to change when I move form grass to gravel. I want the sound effect to change. The script works as far as compiler errors. TRge only problem is when I enter the collision box to mark the gravel sound, the grass sound just keeps playing and the gravel sound never starts. Please help. This is my code:

 #pragma strict
 var Sound : AudioClip;
 var Sound2 : AudioClip;
 
 
 function Update () {
 
     if(Input.GetKeyDown("w")){
       audio.clip = Sound;
       audio.Play();
     }
     if(Input.GetKeyUp("w")){
        audio.Stop();
       }
 }
 
 function OnCollisionEnter(collision : Collision){
 
     if(Input.GetKeyDown("w")){
        
     }
          if(collision.gameObject.tag == "Gravel"){
          audio.Stop();
          audio.clip = Sound2;
          audio.Play();
        }
  
     else if(Input.GetKeyUp("w")){
        audio.Stop();
       }
 }

in future, please format your code by highlighting then clicking the 101010 button, and use better tags. Alucard Jay

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jul 10, 2013 at 06:52 PM 0
Share

You really should be editing that question ins$$anonymous$$d of asking a new one. That is the reason you were downvoted. Now you run the risk of being downvoted again for asking a duplicate question, as well as not formatting your code correctly. Please edit your other question with all this information, then delete this question before you get downvoted, ok. If you do, I will upvote that other question.

  • http://answers.unity3d.com/page/newuser.html

  • http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

Edit : Ok, too late now, it looks like we have a duplicate question situation.

http://answers.unity3d.com/questions/489725/creating-a-walk-sound-that-changes-depending-on-th.html

avatar image Supercrusher9000 · Jul 10, 2013 at 07:57 PM 0
Share

Ok I just deleted the other one.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiloblargh · Jul 10, 2013 at 07:12 PM

Because Input.GetKeyDown is only true for one frame. ( It says so right there in the first sentence, if you had just RTFM.) The code you posted will only change the sound to gravel if you hit "w" at exactly the same time as you first touch a gravel object. If you managed to do that, and then hit w again, it would change back to the original sound, because OnCollisionEnter is also only true one frame and Update happens every frame.

Also, never capitalize a variable name.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Supercrusher9000 · Jul 10, 2013 at 08:03 PM 1
Share

Ok I changed the code. But now theres a compilier error that says " An instance of "UnityEngine.Coomponet" is required to acess a non static member 'gameObject'." #pragma strict var Sound : AudioClip; var Sound2 : AudioClip;

function Update () {

 if(Input.Get$$anonymous$$eyDown("w")){
   audio.clip = Sound;
   audio.Play();
 }
      if(Collider.gameObject.tag == "Gravel"){
      audio.Stop();
      audio.clip = Sound2;
      audio.Play();
    }
 
 if(Input.Get$$anonymous$$eyUp("w")){
    audio.Stop();
   }

}

avatar image KnC_Studios · Aug 18, 2013 at 02:43 AM 0
Share

Change the line if(Collider.gameObject.tag == "Gravel") to this: if(Collider.GameObject.tag == "Gravel")

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

HELP!!! My built game crashes when a trigger is hit 1 Answer

A way to combine 2 or more objects to make 1? 1 Answer

audio on collision 1 Answer

Audio on collision is not playing 1 Answer

de/activate audio listener via collision? (or click?) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges