Unity 3.x Game Development Essentials C# Visual Studio

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?

All code:

using UnityEngine;
using System.Collections;
using UnityEditor;


public class PlayerCollision : MonoBehaviour {

  bool doorisOpen = false;
  float doorTimer = 0;
  public float doorOpenTime = 3;
  public AudioClip doorOpenSound;
  public AudioClip doorCloseSound;
 
 
  ControllerColliderHit gameHit;

  // Use this for initialization
  void Start () {

 
   }
 
   // Update is called once per frame
   void Update () {
 
   }

  void OnControllerColliderHit(ControllerColliderHit hit)
  {
  if (hit.gameObject.tag == "playerDoor" && doorisOpen == false)
  {
  OpenDoor(hit.gameObject);
  }
  }
  void OpenDoor(GameObject door)
  {
 
  doorisOpen = true;
  door.audio.
 
 
 
  }
}

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.

Unity 5.3.2f1 (64-bit).
But when I dosomething like that:

using UnityEngine;
using System.Collections;
using UnityEditor;


public class PlayerCollision : MonoBehaviour {

  bool doorisOpen = false;
  float doorTimer = 0;
  public float doorOpenTime = 3;
  public AudioClip doorOpenSound;
  public AudioClip doorCloseSound;
  AudioSource audio;


  ControllerColliderHit gameHit;

  // Use this for initialization
  void Start () {

  audio = GetComponent<AudioSource>();

   }

   // Update is called once per frame
   void Update () {

   }

  void OnControllerColliderHit(ControllerColliderHit hit)
  {
  if (hit.gameObject.tag == "playerDoor" && doorisOpen == false)
  {
  OpenDoor(hit.gameObject);
  }
  }
  void OpenDoor(GameObject door)
  {

  doorisOpen = true;
  audio.PlayOneShot(doorOpenSound);


  }
}

That working…

it looks like to me that the door does not have a component AudioSource :frowning: How fix this?

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.

Maybe try adding an Audiosource component to your door game object in your project?

Try code tags:

Your “door” variable is a GameObject. There’s no “audio” property on it.

how to solve this? :slight_smile:

I’m try using somthing like that, but still did not work:

door.GetComponent<AudioSource>();
        door.audio.
        doorisOpen = true;
        audio.PlayOneShot(doorOpenSound);

I have the solution!!! ;D

door.GetComponent<AudioSource>().PlayOneShot(doorOpenSound);

That first line does nothing. Why get a component when you’re not storing the result‽
The second line is unfinished.

That works, but now you’re getting the component every time you play the sound. You should cache it sort of like this:

// Inside your class definition
private AudioSource doorSound;
// In Start()
doorSound = door.GetComponent<AudioSource>();

Then

doorSound.PlayOneShot(doorOpenSound);
2 Likes

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.

Thanks a Lot :slight_smile:

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 :

void OpenDoor(GameObject door)
  {
  AudioSource audio = door.GetComponent<AudioSource>().;
  doorisOpen = true;
  audio.PlayOneshot(doorOpenSound);



  }

To do it the way you expected it you could rewrite your code to this:

void OnControllerColliderHit(ControllerColliderHit hit)
  {
         GameObject door = hit.gameObject;
         if (door.tag == "playerDoor" && doorisOpen == false)
         {
             AudioSource audio = door.GetComponent<AudioSource>();
             door.audio.PlayOneShot(doorOpenSound);
         }
  }