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 Ouija · Feb 18, 2014 at 08:58 AM · raycastmultiplesoundsradio

Radio, multiple sound clips script not working

Hey guys, I have this script, it doesn't seem to recognize the raycast, it plays sounds every time I press Z, not what I want. I only want it to happen when you touch the object with the script applied. I can't seem to figure out the structure

 var sound1: AudioClip;
 var sound2: AudioClip; 
 var sound3: AudioClip;
 var sound4: AudioClip; 
 private var radio = "off";
  
 function Update(){
  
 
 
 
 if(Input.GetKeyDown("z")){
  
    
 
 {
 
 
  var hit : RaycastHit;
         
 if (Physics.Raycast(transform.position, 
 transform.forward, hit, 0.2F))
   {   
 
 
 }
 
 
     if(radio == "off"){
     radio = "sound1";
     }
  
     else if(radio == "sound1"){
     radio = "sound2";
     }
     else if(radio == "sound2"){
     radio = "sound3";
     }
     else if(radio == "sound3"){
     radio = "sound4";
     }
     else if(radio == "sound4"){
     radio = "sound1";
     }
  
  
     if(radio == "sound1"){
     Debug.Log("sound is playing"); 
     audio.clip = sound1;
     audio.Play();
     }
     else if(radio == "sound2"){
     audio.clip = sound2;
     audio.Play();
     }
     else if(radio == "sound3"){
     audio.clip = sound3;
     audio.Play();
     }
     else if(radio == "sound4"){
     audio.clip = sound4;
     audio.Play();
     }
   }
 }
 
 }
  
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by POLYGAMe · Feb 18, 2014 at 09:48 AM

It could just be the odd formatting, as not all the code is displaying properly as code here (make sure you hilight it all before hitting that binary button) but it looks as though you're doing nothing inside the if raycast hit statement. You're opening and closing brackets and the code will skip straight past. You need to put the other if statements within that one.

Comment
Add comment · Show 6 · 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 Ouija · Feb 18, 2014 at 09:57 AM 0
Share

Thanks for the input, I'm terrible at writing scripts every time I rearrange the brackets i get error about them, nooo idea where they go lol :(

avatar image POLYGAMe · Feb 18, 2014 at 10:20 AM 0
Share

I find formatting like this helps me keep track of brackets:

 if (b$$anonymous$$yBool)
 {
      //do stuff
 }

Remember to accept answers and vote up anything that's helpful :)

avatar image Ouija · Feb 18, 2014 at 10:24 AM 0
Share

I am lost sorry. I'm not sure if the script even works, I found it on here elsewhere then I tried to add raycast myself.. its all beyond me. The sounds still play anytime I press Z, ins$$anonymous$$d of only when I touch object. $$anonymous$$a brainnnnnn hurts now

avatar image POLYGAMe · Feb 18, 2014 at 10:26 AM 0
Share

I suggest researching raycasts and collisions in the docs, maybe even start with some simpler tutorials to get your head around basic scripting.

avatar image POLYGAMe · Feb 18, 2014 at 10:29 AM 0
Share
  1. is a short ray, too. Is your obstacle right in front of the raycasting object?

Show more comments

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

19 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 avatar image avatar image

Related Questions

Multiple linecasts question 1 Answer

How do you prevent audio from being played multiple times when using raycast? (JS) 1 Answer

How to play sound after another with one button (and after full round continue from where the clip stopped)? 0 Answers

Raycast with multiple camera's in scene 0 Answers

Multiple objects with OnMouseDown 2 Answers


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