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 8r3nd4n · Nov 11, 2013 at 02:41 PM · audiogetcomponentinstancestatic-variables

Using multiple instances of the same script

I am making a game with multiple audio tracks that a player can turn on and off via hitting or missing certain colliders. There are 4 layers of audio tracks that I have working well but there is a script that needs to be applied to all layers yet only affect the sounds of the particular track it resides on.

What I have is a parent object called Layer1, Layer2, etc. that have an Audio Source on it. These have a child object of a collider that will set the audio playing if hit. At the moment every time I hit an object with this script, it starts the track again. I want it that if another object has been hit and set the track playing, then if this one gets hit it will ignore it. This script is as follows:

 public class PlayNote : MonoBehaviour {
     
     public GameObject sound;
 
     void Start () 
     {
         sound = GameObject.Find(this.transform.parent.name.ToString());
         sound.GetComponent("AudioSource");
     }
     
     void OnTriggerEnter(Collider other) 
     {
         if(!AlreadyPlaying.isPlaying)
         {
             AlreadyPlaying.isPlaying = true;
             sound.audio.Play();
             this.gameObject.SetActive(false);
         }
         if(AlreadyPlaying.isPlaying)
         {
             this.gameObject.SetActive(false);
         }
     }
 }



I have a child of this that also has a collider that when hit will stop the audio playing:

 public class StopPlaying : MonoBehaviour {
 
 public GameObject sound;
 
 void Start()
 {
     sound = GameObject.Find(this.transform.parent.transform.parent.name.ToString());    
     sound.GetComponent("AudioSource");
 }
 void OnTriggerEnter(Collider other)
 {
     AlreadyPlaying.isPlaying = false;
     sound.audio.Stop();
 }
 }

The last script is the one that I can't get to work with it all and this is the one that tells the other scripts weather or not the track is already playing and sits on the Layer1/Layer2 game objects that contain the sound:

 public class AlreadyPlaying : MonoBehaviour {
 
 public bool isPlaying;

 void Start () 
 {
 isPlaying = false;
 }
 }

When it runs, the AlreadyPlaying script affects all instances of the bool, not the one specific to the gameobject that the script is sitting on. I have tried using GetComponent("AlreadyPlaying") but I cant seem to access the boolean on each specific layer.

Any help much appreciated.

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
Best Answer

Answer by Tomer-Barkan · Nov 11, 2013 at 02:47 PM

Check my answer in another post here, I think it's a very similar scenario to yours.

You need to create a script attached to your player, that turns on the audio source it collides with, and keeps a reference to the current playing source. Then, when it hits another audio source, you turn off the currently playing source (that's why you kept the reference), and turn on the new source, and also keep a reference to the new source.

Comment
Add comment · Show 1 · 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 8r3nd4n · Nov 27, 2013 at 11:55 AM 0
Share

Sorry for the late kudos, been flat out. That works for keeping the current track playing but I am after tracks to be able to be layered so I need the tracks to be independent of the player, and also that the track sits in a parent of the collidable object. I have managed to get what I was after by using a combination of transform.parent, GetComponent<"LayerName"> with some string manipulation to identify which layer each instance of the script is on.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Slender like game 3 Answers

What does the numSamples parameter in GetOutputData actually mean? 1 Answer

NullReferenceException Issues 3 Answers

How to change monster sounds based on distance? 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