- Home /
 
 
               Question by 
               antonychan0395 · Feb 03, 2020 at 09:19 PM · 
                answers  
              
 
              I cant shoot the ammo when i trigger the box
Why it cant shoot the ammo? I have already put break under the condition
 void Update()
 {   ammo2time += Time.deltaTime;
     gameObject.transform.position += new Vector3(0, -0.02f, 0);
 }
 void Switch()
 {
     switch (caseswitch)
     {
         case 0:
             break;
         
         case 1:
             if (ammo2time > 0.30f)
             {
                 Vector3 Bullet_pos = Ship.transform.position + new Vector3(0, 0.6f, 0);
                 Instantiate(ammo2, Bullet_pos, Ship.transform.rotation);
                 ammo2time = 0;
             }
             
             return;
     }
 }
     void OnTriggerEnter2D(Collider2D col) 
     {
         if (col.tag == "plane") 
         { 
             Destroy(gameObject); 
             caseswitch = 1;
         }
         }
     }
  
 
              
               Comment
              
 
               
              Answer by logicandchaos · Feb 04, 2020 at 01:26 AM
You do set your variable caseswitch to 1, but I can't see if you call your method Switch(). break breaks out of your switch statement, you should use break rather than return for your 2nd case, and you need a default case. 
default: 
break; 
Then your code should work fine. 
it still can't
 // Start is called before the first frame update
 void Start()
 {
 }
 // Update is called once per frame
 void Update()
 {   ammo2time += Time.deltaTime;
     gameObject.transform.position += new Vector3(0, -0.02f, 0);
 }
 void Switch()
 {
     switch (caseswitch)
     {
         case 0:
             break;
         
         case 1:
             if (ammo2time > 0.30f) 
             {
                 Vector3 Bullet_pos = Ship.transform.position + new Vector3(0, 0.6f, 0);
                 Instantiate(ammo2, Bullet_pos, Ship.transform.rotation);
                 ammo2time = 0;
             }
             
             break;
         default:
             break;
     }
 }
     void OnTriggerEnter2D(Collider2D col) 
     {
         if (col.tag == "plane") 
         { 
             Destroy(gameObject); 
             caseswitch = 1;
         }
         }
     }
                 You still don't call Switch() method. Seems it should be called from Update() or right after caseswithc = 1;
Your answer
 
             Follow this Question
Related Questions
Unity Answers or Unity Forum? 1 Answer
Camera following a cannonball that is mouse controlled. 3 Answers
Strange happenings with my code 1 Answer