- Home /
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.
Answer by syclamoth · Jan 31, 2012 at 12:27 AM
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!).
Thanks for the reply,
I tried what you said but there seems to be an issue with calling AudioSource.
I'll try to explain things better now that I understand the system more.
This is what triggers the sound in the systemscript, I need to keep this here.
if (Random.valueStuff > lightningStrikeOdds) { audio.PlayOneShot(lightningSound[Random.Range(0,lightningSound.Length)]); audio.Play();
I need a var AudioSource[] because I want to be able to add as many sounds as desired.
I have this at the top of the systemscript, but it'll error if I take it out. I basically need a audiosource float, or some way of grabbing the sound files from the sound manager when the logic above happens.
var lightningSound : AudioSource[];
It's also not being displayed because it's being called through an editor script.
I need the soundmanager script to display the var lightningSound AudioSource[] columns so the systemscript can play the lightningSound when the logic tells it to. When PlayOneShot happens I need it to grab it from the soundmanager. This way the soundmanager will be able to display the sound column
I still don't follow. What object do you want to hold the array of audiosources in? What do you mean by 'audiosource float'? That's a phrase that doesn't make much sense on any level. You don't need an array of 'AudioSource' components, you need an array of 'AudioClip' objects- one of these is a sound, the other is the device which can play a sound.
Basically, I want to know what the structure of your system is. What objects talk to what, and so on. Remember that you can only put behaviour into editor scripts, you can't use them to store data that you want to use in your game. All they should be able to do is modify assets or objects that will be serialized and put into the game. They can't contain anything be that logic or data, that you want to be accessible from within the finished product.
There isn't really an object it's an empty game object called soundmanager that I add the soundmanager script to. I want the var lightningSound : AudioSource[]; to be in the sound manager script.
I didn't mean editor file, I know they only support guis and what not.
$$anonymous$$y systemscript needs the audio sound to play when the audio.PlayOneShot happens. I am not able to get the AudioSource[] column to display on the systemscript after I compile the script. It's just not there. It can't play sounds that it can't find.
So that's what the soundmanager is for. It will act as the AudioSource[] for the systemscript.
The only thing the soundmanager has is: var lightningSound : AudioSource[];
It's creating a sound column for the systemscript when it needs a sound. But how can I get a sound from the soundmanager TO the systemscript when playOneShot happens?
So what I need help with is when the system script does this:
audio.PlayOneShot(lightningSound[Random.Range(0,lightningSound.Length)]); audio.Play();
I need to get the lightningSound from the soundmanager AudioSource[] even though the systemscript is calling it. It needs to be grab it from the sound manager not the system script.
I see. I think you are using the wrong type in your array- 'lightningSound' should be this:
var lightningSound : AudioClip[];
not this
var lightningSound : AudioSource[];
This way, the line
audio.PlayOneShot(lightningSound[Random.Range(0,lightningSound.Length)]);
will make sense. You don't need the 'audio.Play()' bit after that, by the way- in fact, using that will cause more problems because it will play the default sound for the audiosource, not the one chosen out of the lightningSound array.
Why are you storing the sounds in a different object like this? What advantages does that afford you? In any case, you can always just use
var lightningSounds : soundmanager;
and then assign that in the inspector. Then when you want to get the sounds, use
lightningSounds.lightningSound
to retrieve the array.
I think you don't really understand how this stuff all goes together.
Just because you declared the same named variables on each component, there's nothing that magically connects the two. The variable 'soundCalculator' on the systemscript is not the same variable 'soundCalculator' on the soundmanager.
Using the equality operator on a float is dangerous, in any case- you can never be quite sure with floats (because they aren't precise).
If your 'systemscript' is on an object called "System", then you can get it using
GameObject.Find("System").GetComponent(systemscript);
I'm not sure what the point of this 'soundCalculator' variable is, so I'm going to replace it with a keyboard input for simplicity's sake.
You should put the array of audioClips on the 'System' object, and then make sure that you have an audioSource on your soundmanager object, so that this line will work (in Update)
if(Input.GetButton("Jump"))
{
var sounds : AudioClip[] = GameObject.Find("System").GetComponent(systemscript).lightningSound;
audio.PlayOneShot(sounds[Random.Range(0,sounds.Length)]);
}
I still don't understand how the parts of this system fit together. What is the point of the 'systemscript'? Is it the thing that commands the sounds? Why can't you put all your behaviour in one script, and bypass all of this?
Your answer
