- Home /
Battery Collect Player Collisions Script Problems!
It throws no errors up for me but still when i start the game the battery 2d gui dissapears and when a walk into the battery nothing happens here is my player collisions script and my battery collect script.
private var doorIsOpen : boolean = false; private var doorTimer : float = 0.0; private var currentDoor : GameObject;
var batteryCollect : AudioClip; var doorOpenTime : float = 3.0; var doorOpenSound : AudioClip; var doorShutSound : AudioClip;
function Update () { if(doorIsOpen){ doorTimer += Time.deltaTime; if(doorTimer > 3){ shutDoor();
doorTimer = 0.0;
}
var hit : RaycastHit;
if(Physics.Raycast (transform.position,
transform.forward, hit, 5)) {
if(hit.collider.gameObject.tag=="outpostDoor" && doorIsOpen == false){
currentDoor = hit.collider.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor);
GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=false;
}
}
} } function OnControllerColliderHit(hit: ControllerColliderHit){
if(hit.gameObject.tag == "outpostDoor" && doorIsOpen == false){
currentDoor = hit.gameObject;
Door(doorOpenSound, true, "dooropen", currentDoor); } }
function OpenDoor(){ audio.PlayOneShot(doorOpenSound);
doorOpen = true;
var myOutpost : GameObject = GameObject.Find("outpost");
myOutpost.animation.Play("dooropen");}function shutDoor(){
audio.PlayOneShot(doorShutSound); doorIsOpen = false;
var myOutpost : GameObject = GameObject.Find("outpost");
myOutpost.animation.Play("doorshut"); }
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)
and here is my battery script
static var charge : int = 0;
var charge1tex : Texture2D; var charge2tex : Texture2D; var charge3tex : Texture2D; var charge4tex : Texture2D; var charge0tex : Texture2D;
function Start(){
guiTexture.enabled = false; charge = 0; }
function Update () { 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; } }
All of your code segments need to be surrounded by
[...]
tags to make it easier to read.
Answer by RPH · Nov 10, 2010 at 10:13 PM
First, make sure that you assigned the right tag to the battery objects and that they are all tagged as "battery".
I had the same problem working this example and what i found isnt so much an error on your part as a glitch in unity, so this is what solved it for me. Select one of your battery objects in the scene, in the Inspector you should have a Capsule Collider component, if you followed the example correctly. In this component is the "Is Trigger" checkbox, chances are this box is already checked, but are the words "Is Trigger" in bold?????? if not, simply decheck the box next to Is Trigger and recheck it again and "In Trigger" should now be in bold. Hopefully this helps, thats what did it for me, i think its a glitch in unity.
Your answer
Follow this Question
Related Questions
How do I stop an immediate collision with all objects from ocuring at the entry of game mode? 0 Answers
Player Dies when Collide with enemy 2 Answers
Mesh separating from CharacterController 0 Answers
How to turn of collision for the player 1 Answer
Only targeting the targets root of vector 3 and not colliding with its mesh 1 Answer