If a number reaches a certain value send it to another script to play a sound??

Hi,

I guess this question is really hard to word right so I’ll give it one more shot. Hopefully one of you guys are skilled enough to get this working for me and it would be realllly appreciated. I will give best answer if you get it working for me.

So, we’re developing this system and we have created an editor to simplify things. Everything works fine, but when we converted to the editor we lost the ability to add sound files to the original systemscript.

We want the editor to work with our system so badly but there’s hardly documentation on the editor and we have gone through every single piece of it 10x.

There is no way of getting a AudioClip sound column in the script we have, we have tried. When we remove the editors it appears again, therefore it’s some issue with the editor so that’s not a solution. Unless there is something I don’t know about…

For the explanation.

Script is called…System

//I dont know how to initiate the variable so tell me if I’m doing it wrong…

var numberTest : float; //???

if ((Time.time - lastTime) > lightningQuickness)

{

//Set numberTest equal to 1, If not how would I make this = 1??????
numberTest = 1;

}

Another Script…Called…SoundManager

var lightningSound: AudioClip;

if (numberTest = 1)

{

//If numberTest equals 1 from OTHER script play lightningSound.

audio.PlayOneShot(lightningSound[Random.Range(0,lightningSound.Length)]);

}

My issue is I need to get numberTest FROM the system script TO the soundmanager script. This is taking care of the logic.

So how can I get numberTest to equal 1 in the SYSTEM script then get that value to SOUNDMANAGER to a sound when its reached.

I don’t know how to do this that’s why I’m asking. Please show me an example to get one variable to equal a value and then get it to another script keeping that value.

Hopefully this makes sense and someone ecan help us out. It would be really appreciated. This is the last thing and we’re done, we have put a good 100+ hours into this system.

I’m afraid I don’t quite understand the question. Could you clarify a few things?

You have an array of sounds, yes? Are they on a different object from the audioSource?

You seem to assume that ‘GetComponent(AudioSource)’ will return an array of AudioClips. Obviously that won’t work, because GetComponent returns a component, not an array. Even then, an AudioSource component can only ever hold one sound at a time- you need to code an external manager if you need to play several random sounds.

As far as I can tell, what you want is something along the lines of this:

(in a script on the same object as the audiosource)

var lightningSound : AudioClip[] = GameObject.Find("System").GetComponent(SystemScript).lightningSound;
audio.PlayOneShot(lightningSoundThing[Random.Range(0,lightningSound.Length)]);

This will find the object with the ‘SystemScript’ component (the one which holds the array of audioclips), and grab the array from it. I am assuming that the object is called “System”, but you can substitute that with whatever it’s actually called.

Another thing, that may be a problem, is that you seem to be assuming that you can store data in editor scripts. If you are attempting to store the sounds in the custom editor, they will disappear as soon as you deselect the object. You need to serialize them in some way on the object being inspected, otherwise Unity won’t remember the values. If you want to test out the sounds by pressing a button, you can always call a function on the ‘target’ variable which controls that logic.

Basically, I’m not entirely certain what you are trying to do. I don’t expect you to post your entire script, but at the same time you shouldn’t be afraid to post your logic (because by definition if you need help with it here, it’s not like people are going to be stealing it!).