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 Rich_XR · Nov 14, 2020 at 06:00 PM · raycastaudiotriggerother scriptother-object

Raycast to trigger audio from other script on hit?

Hello,

I am having difficulty in understanding how to implement a raycast on one game object, on hit, to trigger audio on another gameobject from another script

This is my raycast script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Test01 : MonoBehaviour {
 
     public LayerMask mask;
     
     AudioSource audio;
     
     
     void Start() 
     {
         // need to access script attached thot his cube // this could be used for all cubes in the 1000
         audio = GameObject.FindGameObjectWithTag("Cube1").GetComponent<AudioSource>();
         //audio = Cube1.GetComponent<audio>();
     }
 
     void Update () {
 
         Ray ray = new Ray (transform.position, transform.forward);
         RaycastHit hitInfo;
 
         if (Physics.Raycast (ray, out hitInfo, 100, mask, QueryTriggerInteraction.Collide)) {
             print (hitInfo.collider.gameObject.name);
             //Destroy (hitInfo.transform.gameObject);
 
             if (hitInfo.collider.isTrigger)
             {
             
             Debug.DrawLine (ray.origin, hitInfo.point, Color.red);
             //GetComponent<AudioSource>().PlayOneShot ( mySound);
             audio.PlayOneShot(mySound);
 
             }
 
         } else { 
 
             Debug.DrawLine (ray.origin, ray.origin + ray.direction * 100, Color.green);
         }
         
     }
 }

This the audio script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class audio : MonoBehaviour
 {
     public AudioSource myAudioSource; // a private reference to an AudioSource variable
     public AudioClip mySound; // a public reference to an AudioClip (sound)
  
 // Start is called before the first frame update
 void Start()
 {
     myAudioSource = GetComponent<AudioSource>(); // finds the AudioSource component within the GameObject
 
 }
 // OnTriggerEnter is called when an actor enters the collider
 void OnTriggerEnter(Collider other)
 {
     myAudioSource.clip = mySound;
     myAudioSource.Play();
 }
 }
 

I feel I am using the wrong trigger maybe? also dont know how to call/reference properly from another script when, i thought calling through getcomponent would do this

I am getting errors saying context? " Assets/Scripts/Test01.cs(33,22): error CS0103: The name 'mySound' does not exist in the current context"

Apologies I am quite new to c#. The desired behaviour is for the raycast to just trigger every time it hits one of this gameobject

Thanks in advance

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by dylanfries · Nov 14, 2020 at 08:16 PM

Hi,

You might want to post more info about the error, its not clear what the problem is. I would also be careful destroying the hit object before you are done with it, perhaps it is affecting hitInfo (which references it).

Also, quick trick, you might want to look into Debug.DrawRay as well as it lets you pass the ray itself directly in to draw and can be useful for debugging what is actually being cast when you raycast.

Comment
Add comment · 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
0

Answer by Rich_XR · Nov 15, 2020 at 10:29 AM

I have edited the question and included the errors as requested.

Comment
Add comment · 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

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

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

Related Questions

Continuous RayCast on GetButtonDown 0 Answers

Play different sounds on different surfaces? 1 Answer

Trying to play other objects' sound and animations from Raycast hit 1 Answer

How can i make only some Raycasts hit triggers 1 Answer

help with multiple sounds 0 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