Need Help with PlayerCollisions.cs from Unity 3.x Development Essentials.

First time I posted in a while (as far as I can remember).

I’m currently working on the book Unity 3.x Development Essentials, and I’ve come to an impasse with some of the scripting.

I’ve chose to use C# Script. I’m to create the PlayerCollisions script in C#. I got to the part just before ray casting, so I’m dealing with boundary collisions. The problem is the statement GameObject currentDoor; . I’m not sure where to exactly put the piece of code, considering I’m still learning C# (I though Unity would be a fun way to learn the language). I’ll post the code below, and I hope someone has an answer for my problem.

using UnityEngine;
using System.Collections;

public class PlayerCollisions : MonoBehaviour {
	
	bool doorIsOpen = false;
	float doorTimer = 0.0f;
	public float doorOpenTime = 3.0f;
	public AudioClip doorOpenSound;
	public AudioClip doorShutSound;
	GameObject currentDoor;
		
	// Use this for initialization
	void Start () {
	
	}
	
	void OpenDoor(GameObject door){
		doorIsOpen = true;
		door.audio.PlayOneShot(doorOpenSound);
		door.transform.parent.animation.Play("dooropen");
	}
	
	void ShutDoor(GameObject door){
		doorIsOpen = false;
		door.audio.PlayOneShot(doorShutSound);
		door.transform.parent.animation.Play("doorshut");
	}
	
	void Door(AudioClip aClip, bool openCheck, string animName, GameObject thisDoor){
		thisDoor.audio.PlayOneShot(aClip);
		doorIsOpen = openCheck;
		thisDoor.transform.parent.animation.Play(animName);
	}
	
	// Update is called once per frame
	void Update () {
		
				if(doorIsOpen){
			doorTimer += Time.deltaTime;
		}
		if(doorTimer > doorOpenTime){
			ShutDoor(currentDoor);
			doorTimer = 0.0f;
		}
	}
					
	void onControllerColliderHit(ControllerColliderHit hit){
		if(hit.gameObject.tag == "playerDoor"  doorIsOpen == false){
			OpenDoor(hit.gameObject);
			Door(doorOpenSound, true, "dooropen", currentDoor);
		}

	}
		
}

Well, I’m not sure what your question is, but is it “where do you have to set the currentDoor variable?” ?

I think then the solution to your problem is, that you should set the variables (doorTimer, currentdoor, etc) in the OpenDoor function.

It seems pahe the biggest problem is there’s no variable for currentDoor - or at least I didn’t see it listed in the book I’m using. So I don’t know what variable to use.

And I’ll give the program a try with the variables in the OpenDoor function - I know I haven’t tried that yet.

But at least I’m doing my homework on this - I just have this one problem I can’t solve yet.

:slight_smile:

I think you will want to add this line of code to your OpenDoor function:

currentDoor = this.gameObject; // Or this: currentDoor = door;

That’s what Pahe means.

Thanks MaarX. I’ll go ahead and try that. Wish me luck!!!

I got the code to work MaarX - in the sense that I get no compiler errors. From what I’ve read, the doors on my outpost should now animate with accompanying sounds, but all I can do is walk up to the door - and nothing happens. I’ll have to go back and figure out what I did wrong. Maybe I need to add a little bit more coding… :face_with_spiral_eyes:

Well, got that last bit of code, and I added other coding to the ‘game’ prototype I’m making based off the back. I know can get the door on my outpost to open and close, as well as any sounds that accompany the movement of the door. That little bit of code was all that was holding me back.

Thank you guys, I appreciate the help. :smile:

I’m needing more help. I have my code typed in correctly for audio attached to my outpost’s door, but I keep getting an error message saying there’s no audio file attached to the door when there is some audio attached to it.

Here’s the code for the DoorManager script, which handles the doors behavior, as well as the audio.

using UnityEngine;
using System.Collections;

public class DoorManager : MonoBehaviour {
	
	bool doorIsOpen = false;
	float doorTimer = 0.0f;
	public float doorOpenTime = 3.0f;
	public AudioClip doorOpenSound;
	public AudioClip doorShutSound;
		
	// Use this for initialization
	void Start () {
	doorTimer = 0.0f;
	} 
	
	// Update is called once per frame
	void Update () {
	
	if(doorIsOpen){
			doorTimer += Time.deltaTime;
			if(doorTimer > doorOpenTime){
				Door(doorShutSound, false, "doorshut");
				doorTimer = 0.0f;
			}
	}
}
	
	void DoorCheck(){
		if(!doorIsOpen){
			Door(doorOpenSound, true, "dooropen");
		}
	}
	
	void Door(AudioClip aClip, bool openCheck, string animName){
		audio.PlayOneShot(aClip);
		doorIsOpen = openCheck;
		transform.parent.gameObject.animation.Play(animName);
	}
}

I have a both a “doorshut” and “dooropen” audio files attached to the door under the name of this script, but I still get the error message. Does anyone know what kind of mistakes I could be making that would cause this to happen?

I forgot to add what the message said:

MissingComponentException: There is no ‘AudioSource’ attached to the “door” object, but the script is trying to access it.

Maybe I need to attach an AudioSource from the top menu to get it to work…

Yes, that’s what was wrong. Sorry guys.