1

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!

1

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You might also want to checkout this link.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.