Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Will21 · Dec 13, 2014 at 11:30 AM · javascriptcollider

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");
       }
  } 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges