Multiple tags for game object – in progress

So I don’t know if this is good or bad form, but I’d really like to be able to tag all of the gameobjects that should have audio played as “Audio.”  The other option is to specify each one, which would be to specify these tags in the script : “Onset,” “Ending,” and “Answer.”

Honestly maybe I’ll just do that to start.  Tagging them with “Audio” seems a little redundant.   Ohhh I remember why I can’t do it that way.  I want to tag the objects themselves in the scene – I don’t want the script to have to do it, or else I’ll have to have different scripts for every type of audio scene, and that’s soooo redundant and clunky.

Okay so I went ahead and tried it the way I described above, even though it’s not what I eventually want.

This works.  A problem that I will encounter is that other parts of the script look for the audio retrieved in this.  MissingComponentException: There is no ‘AudioSource’ attached to the “2” game object, but a script is trying to access it.
You probably need to add a AudioSource to the game object “2”. Or your script needs to check if the component is attached before using it. PlayerCoroutine+<OnTriggerEnter2D>c__Iterator0.MoveNext () (at Assets/Scripts/PlayerCoroutine.cs:82)

Perhaps I still put the audio component on, but just don’t play it?  That might be the easiest so that I only have one of those if{} loops.

Oops no I’ll still need multiple if{} loops, because then it’ll still say the onset when it lists them out in the coroutine.

    // when object is clicked, play its sound 
    void OnMouseDown()
    {
        string[] audios = { Ending, Answer }; // this forces the scripts to be different, as opposed to having the tags be different
        foreach (string audio in audios)
        {
            
            if (gameObject.tag == audio)
            {
                PlayAudio();
            }
        }

    }

A horrible thing I could do is instead of having multiple tags on an object, I could have if{} loop at the beginning that defines audios[] by the Scene name.  That could work, and could make it so I don’t have to handtag everything.  That actually might be the best version!  Not sure if it’s best practice, or fast, or anything.  But … gonna try that.

Ugh I need if{} loops for every time I play a thing.  Huh.  I could do something pretty cheaty and load a blank sound if it’s not in audio, and then whenever it plays it, it still exists, but it just plays nothing.  Honestly I think that’s the easiest – only has one if{} loop, and as long as I have a comment documenting why, it shouldn’t confuse any possible future programmers. (right?)

Okay so I just did this, and it works BEAUTIFULLY.   Now just to set up the audios[] definitions in each script that needs it – or … maybe make it accessible from game manager?  Not really sure … but gosh that sounds nice so I only have to define it once, then just put the if loops in what sound file it retrieves in PlayerCoroutine.cs and PlayerFinalWord.cs.

Okay gonna give that a go.

To get the name of the active scene (from this thread):

  1. Scene scene = SceneManager.GetActiveScene();
  2. scene.name; // name of scene

Also this is a good reason to have objects have their audio added in the same way, so that I only have to have one if{} loop for onsets & codas.  I’ll still need a separate one for Answer, cuz that audio gets added in the middle of the game (depending on what letters are put together).

Having the problem I often have, which is that I still don’t quite understand how to make list (#todo).   If I put this at the top,

public string[] audios;

with this in another function,

 

        if (scene.name == 2Practice)
        {
            audios = {Ending, Answer}; 
        }

Then I get this error: Assets/Scripts/GameManager.cs(53,22): error CS1525: Unexpected symbol `{‘

This is because I apparently desperately want to add multiple items to a pre-made list in one line.  Haven’t seen any solutions I love (usually they involve making more functions), so I’m just gonna do it one line at a time.

Cool this is working now, and it’s working calling it from GameManager.  Buuuuut just got a fatal error that stopped the game and had to quit Unity overall (it froze in playmode):

!attach.depth || !attach.depth->backBuffer

This thread thinks maybe it’s a 3rd party asset.  The only one I have in this is CVS2Table, and I’m not using it, so I’ll just delete that.

Ah, just realized it’s not that my if{} loop is working, it’s that it’s only attached to Onsets, and all of them all going through the else{} where it assigns the blank .wav.  Sigh.

Okay, apparently I can’t have an OnClick script attached to a button AND manually enter in what it should do OnClick.  This is sad and gives me an eye twitch.  I want both because I want three columns of buttons – 1st column takes you to 1Learn scene, 2nd column takes you to 2Practice scene, 3rd column takes you to 3ShowOff scene (this was being done in the button’s manual OnClick thing).  But they still need to get the level from the button, which is done through the added script.

Wait nevermind.  Both things TOTALLY work.  YAYYYY.

To check if an item is in a list, List.Contains(item) (from this thread).   #todo – check if I have any foreach() loops that should actually be if (List.Contains(item)) .

 

Leave a Reply

Your email address will not be published. Required fields are marked *