Reputation: 23
I'm trying to make it so when the player presses the RMB, another object, with a different script, plays a noise. There are 5 cars, and you get a random one when the game starts, I only want the car you get picked to make the noise. Is there anyway of doing this? (C#)
Basically, the player gets a car to look for, and when they find it, they win, but they have to find the correct car. You get a random car each time, and I want it so to help the player find the correct car, they can beep the car. I only want the car they are searching for to beep, and I want to call it from one script to another.
Even more basically, I would like to know how to call script to script in C# unity. Thanks!
Upvotes: 1
Views: 488
Reputation: 16
Since you given no code, i'll simply presume that you have those cars that you are clicking as an object. Lets say that your car object is called "carX". It obviously has a script that plays sound, and lets say that script is called "carSoundPlayer".
What you need to do is:
carX.getComponent<carSoundPlayer>().audioSource.Play()
Basically - if you have script attached to any GameObject you can access it by GameObjectName.getComponent()
If you still need help, please provide more details or code example.
Upvotes: 0
Reputation: 2720
What you are looking for is the GetComponent command. You can call it on the car game object then once you have access to the component call any commands you want.
http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
Upvotes: 2