Help - Unity game development essentials

Im having a problem with the battery GUI part. The GUI wont charge for some reason? When the door is approuched the gui and text hint will appear, the batteries can be picked up and make a noise. but the textures dont seem to be changing for some reason. Does anyone know how to fix this? thanks.

these are the scripts being used

private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;

var doorOpenTime : float = 3.0;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
var batteryCollect : AudioClip;

function Update () {
if (doorIsOpen) {
doorTimer += Time.deltaTime;

if (doorTimer > doorOpenTime) {
Door(doorShutSound, false, “doorshut”, currentDoor);
doorTimer = 0.0;
}
}

var hit : RaycastHit;

if (Physics.Raycast (transform.position, transform.forward, hit, 5)) {
if (hit.collider.gameObject.tag == “outpostDoor” doorIsOpen == false BatteryCollect.charge >= 4) {
currentDoor = hit.collider.gameObject;
Door (doorOpenSound, true, “dooropen”, currentDoor);

GameObject.Find(“PowerGUI”).GetComponent (GUITexture).enabled=false;
}else if (hit.collider.gameObject.tag==“outpostDoor” doorIsOpen == false BatteryCollect.charge < 4) {
GameObject.Find(“PowerGUI”) .GetComponent (GUITexture) .enabled = true;
TextHints.message = “The door seems to need more power…”;
TextHints.textOn = true;

}

}

}

/*
function OnControllerColliderHit ( hit : ControllerColliderHit) {

if (hit.gameObject.tag == “outpostDoor” doorIsOpen == false) {
currentDoor = hit.gameObject;
Door(doorOpenSound, true, “dooropen”, currentDoor);
}
}
*/
function Door(aClip : AudioClip, openCheck : boolean, animName : String, thisDoor : GameObject) {
audio.PlayOneShot(aClip);
doorIsOpen = openCheck;
thisDoor.transform.parent.animation.Play(animName) ;
}

function OnTriggerEnter (collisionInfo : Collider) {

if (collisionInfo.gameObject.tag == “Battery”) {
BatteryCollect.charge++;
audio.PlayOneShot (batteryCollect);
Destroy (collisionInfo.gameObject);
}
}

@script RequireComponent (AudioSource)

static var charge : int = 0;

var charge4tex : Texture2D;
var charge3tex : Texture2D;
var charge2tex : Texture2D;
var charge1tex : Texture2D;
var charge0tex : Texture2D;

function Start(){
guiTexture.enabled = false;
charge = 0;
}

if(charge == 1){
guiTexture.texture = charge1tex;
guiTexture.enabled = true;
}

else if(charge == 2){
guiTexture.texture = charge2tex;
}

else if(charge == 3){
guiTexture.texture = charge3tex;
}

else if(charge >= 4){
guiTexture.texture = charge4tex;
}

else{
guiTexture.texture = charge0tex;
}

Hi there, you must have made some form of error before this point as what you have script wise looks right. Are all your scripts attached in the right places etc? and do all of your GUI textures look right? also if you have any issues with the book check the errata pages on packtpub.com’s ‘support’ section.

Would just like to say this book has been very useful. thanks.

Still having problems trying to get it to work. The GUI textures look right. The GUI has the battery_charge script added. All of the textures have been placed in the right order and the fps has the collision script added. Just cant seem to work out why it still wont change the textures?

Have you ensured that the battery objects have ‘is trigger’ ticked? and also that the tags ‘Battery’ are applied to all of them?

yes they are ticked and taged. I’m sure i followed everything in the right steps. Wierd why it wont show.

can you provide any screenshots of your setup using tinypic.com please?

Hey Will!
However great and easy to read and follow the book is, I have the same problem! (I have the older book though, for Unity 2)
I checked my scripts a hundred times, tried to correct them myself, etc but no matter what I do the GUI simply wont pop up!
What should I do? (Note: I havent yet done the “Restricting outpost access” part!) Cheers!

Also, although the “restriction” works (I cant enter until I collected all 4 batteries, although GUI still doesnt show up) I can simply walk through the door as if I was a ghost… :confused:

Okay, Ive spend a lot of time with it now, and to my disappointment it still won’t work…
Here’s what I did:
After having made sure that all the code for collecting batteries is correct etc, in-game when the character walked over the battery a sound would be heard, the battery would disappear, the GUI however wouldn’t work. After the next section (restricting access until you have 4 batteries) I managed to get the 0 GUI to pop up (empty battery) when standing in front of the door (you could however walk through it like a ghost?)
I decided to skip this for now, and go on to the next step with the TextHints. After having done all that, and the GUI still wouldnt show up I decided to copy and paste your code which was supplied along with all the stuff in your book (all those scripts:
/* Script Provided by Will Goldstone as part of Unity Game Development Essentials book assets /
/
Please Do Not Remove this comment - this script is for reference only */)

However, now when I try to press play I get the following error:

Assets/Scripts/PlayerCollisions.js(47,29): BCE0017: The best overload for the method ‘PlayerCollisions.Door(UnityEngine.AudioClip, boolean, String, UnityEngine.GameObject)’ is not compatible with the argument list ‘(UnityEngine.AudioClip, boolean, String)’.

And

Assets/Scripts/PlayerCollisions.js(33,29): BCE0017: The best overload for the method ‘PlayerCollisions.Door(UnityEngine.AudioClip, boolean, String, UnityEngine.GameObject)’ is not compatible with the argument list ‘(UnityEngine.AudioClip, boolean, String)’.

So what on earth do I do? D:
Hope to hear from you soon! Cheers!

Sorry for continuously spamming this thread!
Although I fixed the problem with the door, and advanced onto chapter 6 I ran into a problem here! :confused:

I created the CoconutThrow script and made sure it’s 100% correct. I applied it to the launcher, added the Throw Sound and the Coconut Object and changed the Throw Force.
However, I get this error:

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

It highlights this line:

audio.PlayOneShot(throwSound);

I don’t know what to change! Help?

Read the exception carefully. It tells you exactly what you need to know in plain English.

You need to add an AudioSource component to Launcher. without it, you can’t call audio.anything.

Hey there, I’m doing the same tutorial as you did and I’m having the same problem. Everything is working fine except for the GUI, when i collect the batteries the GUI doesnt increment to the next texture, it just stays on the 0 texture throughout the game.
Did you ever figure out the solution?
If you did could you give me an idea of how to fix this?
I have literally spent about 5 hours trying to fix it!!!
:slight_smile:
p.s my code is the same as yours more or less