-1

I've got a game when the user hits itself to the right or the left edge, there will be a sound, like oph, ouch, Oh No, and so on. I want that to be a random sound (random pick) each time.

here is the piece of code where we determine the sound name. In the following snippet hit1 is the sound name, one of them (as is determined in Elements). This code works well. The code comes from "controller.cs"

Note: Inside Unity there is an area in _Manager Prefab (_Manager/Audios Manager Script/Music Clips/Elements[Audio Clip, Sound Name, Volume]) which this sound name and any other one is listed.

else 
{
    if (!iFly && !iJump)
    {
        animationManager.animationState = animationManager.TurnRight;
        AudiosManager.instance.PlayingSound("hit1");
    }
}

I've been trying to do it like this but it doesn't work by listing remaining sound names "hit1","hit2","hit3","hit4" inside a Random (), like this Random("hit1","hit2","hit3","hit4") hoping it works. But of course it didn't work.

Ok, here is how the code looks like now, which is not working.

else 
{
    if (!iFly && !iJump)
    {
        animationManager.animationState = animationManager.TurnRight;
        AudiosManager.instance.PlayingSound(Random("hit1","hit2","hit3","hit4"));
    }
}

To clarify, really the code responsible for this part is only this one AudiosManager.instance.PlayingSound("hit1");. I could be wrong though.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Malsh
  • 31
  • 11
  • 1
    So, your question is basically how to get a random item from an array? Google this and you should be able to use that knowledge to modify it for this to work – Camilo Terevinto Jun 26 '18 at 01:10
  • 2
    Possible duplicate of [How do I generate a random int number in C#?](https://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c) – Camilo Terevinto Jun 26 '18 at 01:10
  • @CamiloTerevinto It's really hard for me to follow other guides, appreciate a more direct answer, – Malsh Jun 26 '18 at 01:46
  • @Malsh An answer from SO will still only be a guide: we are not a code writing service. – Draco18s no longer trusts SE Jun 26 '18 at 02:45
  • Are you able to play a specific sound effect? – Rodrigo Rodrigues Jun 26 '18 at 03:32
  • @RodrigoRodrigues Yes, when I write like this "AudiosManager.instance.PlayingSound("hit1");" I can play the sound named hit1. However, if I were to play many sound and write it like this "AudiosManager.instance.PlayingSound(Random("hit1","hit2","hit3","hit4"));", I can't play sound, but also get error. – Malsh Jun 26 '18 at 14:04

1 Answers1

1

Solved!
I had to put public string hits; under public class Controller : MonoBehaviour. Like bellow.

public class Controller : MonoBehaviour {

    public string hits;  

And this is how the code looks like now

} else {
            if (!iFly && !iJump){
                string[] hits = { "hit1", "hit2", "hit3", "hit4", "hit5" };  
                animationManager.animationState = animationManager.TurnRight;
                AudiosManager.instance.PlayingSound(this.hits = hits[Mathf.FloorToInt (Random.Range(0,5))]);
            }
        }

So, as you can see, I've added this string[] hits = { "hit1", "hit2", "hit3", "hit4", "hit5" }; to define my strings, any number of them it be, and this this.hits = hits[Mathf.FloorToInt (Random.Range(0,5))] to call a random string each time.

Thansks for all of you who tried to help me in the process!

Malsh
  • 31
  • 11