- Home /
Boxcollider 2D Destroyed
I am making a metroidvania game and its going really but when ever i collect a power up i get this error The object of type 'BoxCollider2D' has been destroyed but you are still trying to access it. How do i fix it because it works perfectly just how i want it the only thing is i get the error, my code(its attached to my player) CAN ANSWERS BE IN JAVA SCRIPT ONLY:
#pragma strict
var Speed : float = 12;
var Health : float = 100;
//Items and ability variables
var ShowLabelSuperboots : boolean = false;
var ShowLabelLaserblaster : boolean = false;
var ShowLabelMegaBoots : boolean = false;
var ShowLabelGevar : boolean = false;
var Skin : GUISkin;
var AbilityLabelTime : float = 3;
var CollectedGun : boolean = false;
var StopTime : float = 3;
//Bullet variables
public var bulletRight : GameObject;
public var bulletLeft : GameObject;
function Update ()
{
//Jump
if (Input.GetKey (KeyCode.W))
{
transform.Translate(Vector3(0,Speed,0) * Time.deltaTime);
}
//Move left
if (Input.GetKey (KeyCode.A))
{
transform.localScale.x = 1;
transform.Translate(Vector3(-Speed,0,0) * Time.deltaTime);
}
//Move right
if (Input.GetKey (KeyCode.D))
{
transform.localScale.x = 1;
transform.Translate(Vector3(Speed,0,0) * Time.deltaTime);
}
//Shoot right
if (CollectedGun == true){
if(Input.GetKey(KeyCode.RightArrow)) {
Instantiate(bulletRight, transform.position, Quaternion.identity);
}
}
//Shoot left
if (CollectedGun == true){
if (Input.GetKey(KeyCode.LeftArrow)) {
Instantiate(bulletLeft, transform.position, Quaternion.identity);
}
}
}
function OnTriggerEnter2D (other : Collider2D) {
//Super speed power up
if (other.gameObject.tag == "SuperSpeed") {
ShowLabelSuperboots = true;
Speed = Speed + 8;
Destroy(other.gameObject);
yield WaitForSeconds(AbilityLabelTime); ShowLabelSuperboots = false;
}
//Laserblaster power up
if (other.gameObject.tag == "Laserblaster") {
CollectedGun = true;
ShowLabelLaserblaster = true;
Destroy(other.gameObject);
yield WaitForSeconds(AbilityLabelTime); ShowLabelLaserblaster = false;
}
//Megaspeed power up
if (other.gameObject.tag == "MegaSpeed") {
ShowLabelMegaBoots = true;
Destroy(other.gameObject);
Speed = Speed + 10;
yield WaitForSeconds(AbilityLabelTime); ShowLabelMegaBoots = false;
}
//Gevar suit power up
if (other.gameObject.tag == "Gevar") {
ShowLabelGevar = true;
Destroy(other.gameObject);
yield WaitForSeconds(AbilityLabelTime); ShowLabelGevar = false;
}
}
function OnGUI() {
GUI.skin = Skin;
//Label Super Boots
if (ShowLabelSuperboots){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Super Boots Acquired - These let you move and jump faster");
}
//Label Laserblaster
if (ShowLabelLaserblaster){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Laser Blaster Acquired - This lets you shoot with the arrow keys");
}
//Label Mega Boots
if (ShowLabelMegaBoots){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Mega Boots Acquired - These let you move and jump even faster");
}
//Label Gevar Suit
if (ShowLabelGevar){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Gevar Suit Acquired - Protects you from lava");
}
}
Answer by Will21 · Dec 13, 2014 at 01:05 PM
I worked it out I used some functions to make the labels appear: #pragma strict
var Speed : float = 12;
var Health : float = 100;
//Items and ability variables
var ShowLabelSuperboots : boolean = false;
var ShowLabelLaserblaster : boolean = false;
var ShowLabelMegaBoots : boolean = false;
var ShowLabelGevar : boolean = false;
var Skin : GUISkin;
var AbilityLabelTime : float = 3;
var CollectedGun : boolean = false;
var StopTime : float = 3;
//Bullet variables
public var bulletRight : GameObject;
public var bulletLeft : GameObject;
function Update ()
{
//Jump
if (Input.GetKey (KeyCode.W))
{
transform.Translate(Vector3(0,Speed,0) * Time.deltaTime);
}
//Move left
if (Input.GetKey (KeyCode.A))
{
transform.localScale.x = 1;
transform.Translate(Vector3(-Speed,0,0) * Time.deltaTime);
}
//Move right
if (Input.GetKey (KeyCode.D))
{
transform.localScale.x = 1;
transform.Translate(Vector3(Speed,0,0) * Time.deltaTime);
}
//Shoot right
if (CollectedGun == true){
if(Input.GetKey(KeyCode.RightArrow)) {
Instantiate(bulletRight, transform.position, Quaternion.identity);
}
}
//Shoot left
if (CollectedGun == true){
if (Input.GetKey(KeyCode.LeftArrow)) {
Instantiate(bulletLeft, transform.position, Quaternion.identity);
}
}
}
function OnTriggerEnter2D (other : Collider2D) {
//Super speed power up
if (other.gameObject.tag == "SuperSpeed") {
ShowLabelSuperboots = true;
Speed = Speed + 8;
WaitAndFalseSuperSpeed ();
Destroy(other.gameObject);
}
//Laserblaster power up
if (other.gameObject.tag == "Laserblaster") {
CollectedGun = true;
ShowLabelLaserblaster = true;
WaitAndFalseLaserblaster ();
Destroy(other.gameObject);
}
//Megaspeed power up
if (other.gameObject.tag == "MegaSpeed") {
ShowLabelMegaBoots = true;
Speed = Speed + 10;
WaitAndFalseMegaBoots ();
Destroy(other.gameObject);
}
//Gevar suit power up
if (other.gameObject.tag == "Gevar") {
ShowLabelGevar = true;
WaitAndFalseGevar ();
Destroy(other.gameObject);
}
}
function OnGUI() {
GUI.skin = Skin;
//Label Super Boots
if (ShowLabelSuperboots){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Super Boots Acquired - These let you move and jump faster");
}
//Label Laserblaster
if (ShowLabelLaserblaster){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Laser Blaster Acquired - This lets you shoot with the arrow keys");
}
//Label Mega Boots
if (ShowLabelMegaBoots){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Mega Boots Acquired - These let you move and jump even faster");
}
//Label Gevar Suit
if (ShowLabelGevar){
GUI.Label(Rect(100,100,Screen.width,Screen.height),"Gevar Suit Acquired - Protects you from lava");
}
}
function WaitAndFalseSuperSpeed () {
yield WaitForSeconds(AbilityLabelTime); ShowLabelSuperboots = false;
}
function WaitAndFalseLaserblaster () {
yield WaitForSeconds(AbilityLabelTime); ShowLabelLaserblaster = false;
}
function WaitAndFalseMegaBoots () {
yield WaitForSeconds(AbilityLabelTime); ShowLabelMegaBoots = false;
}
function WaitAndFalseGevar () {
yield WaitForSeconds(AbilityLabelTime); ShowLabelGevar = false;
}
Answer by jenci1990 · Dec 13, 2014 at 11:39 AM
Use else if:
function OnTriggerEnter2D (other : Collider2D) {
//Super speed power up
if (other.gameObject.tag == "SuperSpeed") {
ShowLabelSuperboots = true;
Speed = Speed + 8;
Destroy(other.gameObject);
yield WaitForSeconds(AbilityLabelTime); ShowLabelSuperboots = false;
}
//Laserblaster power up
else if (other.gameObject.tag == "Laserblaster") {
CollectedGun = true;
ShowLabelLaserblaster = true;
Destroy(other.gameObject);
yield WaitForSeconds(AbilityLabelTime); ShowLabelLaserblaster = false;
}
//Megaspeed power up
else if (other.gameObject.tag == "MegaSpeed") {
ShowLabelMegaBoots = true;
Destroy(other.gameObject);
Speed = Speed + 10;
yield WaitForSeconds(AbilityLabelTime); ShowLabelMegaBoots = false;
}
//Gevar suit power up
else if (other.gameObject.tag == "Gevar") {
ShowLabelGevar = true;
Destroy(other.gameObject);
yield WaitForSeconds(AbilityLabelTime); ShowLabelGevar = false;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Boolean wont acitvate if the enemy enters a trigger 1 Answer
scripting help needed by a Script-illiterate 0 Answers
Problem with Javascript updating variables. 1 Answer
Accessing colliders, in if statement 2 Answers