Hello!
I’m trying to finish a book Unity 3.x Game Development Essentials , but I’m stuck. When i try use somthing like that: door.audio**.PlayOneShot()** i dont see this function. Could you tell me why?
What version of Unity are you using? My guess would be that you are trying to follow along with the book written for Unity 3.x with a newer version of Unity where that function has been deprecated and removed.
No clue… Unity has changed a LOT since 3.x. It is going to be really hard to use a 3.x book to learn Unity 5.x. I’d recommend one of two things, Either go download the last version of 3.x from the Unity site or try to find the same book for Unity 5.x.
This is a direct result of using a book for a very old version of Unity. Specifically, in Unity 5.0, they removed the convenient accessor properties - gameObject.audio, .renderer, .rigidbody, etc. In place of all of these, you need to use GetComponent
They did this because those accessors made the various pieces of the engine too tightly entangled, and removing them allows Unity to compile a game and exclude chunks of the engine and reduce the final build size. For example, there are two physics engines (2D and 3D), and many projects don’t even use one, let alone both. This is a minor issue with standalone builds, but a major issue with WebGL, where the entire engine must be downloaded, in Javascript, every time the user plays the game. The ability to strip the build size was deemed crucial enough to justify the inconvenience of having to type GetComponent more.
Your problem is you are passing the door to the function as a gameObject and most game objects do not have audio functions builtin so you need to set the audio script on the game Object once it’s passed like :