Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by antonychan0395 · Feb 05, 2020 at 06:58 AM · answers

boolean problem

I want to let when my plane touches the powerbox, the powerbox disappears, and the ammo will shoot according to the motion of the plane. However, if I type destroy(gameObject), the function will only destroy the gameobject and the ammo will not shoot. When I delete destroy(gameObject), the powerbox will not be destroyed. But the ammo will only shoot on one side and will not shoot according to the motion of the plane. I have tried so long.

 void Update()
     {
 
  gameObject.transform.position += new Vector3(0, -0.02f, 0);
         ammo2time += Time.deltaTime;
         if (hitpowerbox == true)
         {
             if (ammo2time > 0.30f) 
             {
 
                 Vector3 ammo2_pos = Ship.transform.position + new Vector3(2, 0.1f, 0);
 
                 Instantiate(ammo2, ammo2_pos, Ship.transform.rotation);
 
                 ammo2time = 0;
                 
             }
             else
             { }
 
         }
 
     }
 
     void OnTriggerEnter2D(Collider2D col) 
     {
         if (col.tag == "plane")
         {
 
             hitpowerbox = true;
 
 
         }
         Destroy(gameObject);
     }
Comment
Add comment · Show 8
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 JPhilipp · Feb 05, 2020 at 11:14 AM 0
Share

It seems, as has been mentioned in a previous answer to your question, you're (for starters) destroying the object before it gets a chance to do anything in the next Update.

avatar image ShadyProductions · Feb 05, 2020 at 11:45 AM 0
Share

put the hitpowerbox field on another script on the plane with the update logic ins$$anonymous$$d of the bullet then in ur on trigger enter u can get the plane script using GetComponent and then set hitpowerbox = true on that script.

avatar image antonychan0395 ShadyProductions · Feb 05, 2020 at 07:54 PM 0
Share

it cannot work properly

spawnammo script public bool hitpowerbox = false; public GameObject Ship; public GameObject ammo2; public float ammo2time; void Update() { ammo2time += Time.deltaTime; if (hitpowerbox == true) {

         Vector3 ammo2_pos = Ship.transform.position + new Vector3(2, 0.1f, 0);
         Instantiate(ammo2, ammo2_pos, Ship.transform.rotation);
         
         ammo2time = 0;


     }

 }

}

bonusammo script void Update() { gameObject.transform.position += new Vector3(0, -0.02f, 0);

 }
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.tag == "plane")
         {
         GetComponent<spawnammo>();
         hitpowerbox = true;

         Object.Destroy(gameObject, 0.1f);

         }

     }
 

}

it is not work.

avatar image tormentoarmagedoom · Feb 05, 2020 at 02:54 PM 1
Share

And delay the destriy function?

public static void Destroy(Object obj, float t = 0.0F);

So

 Destroy (Object, 3);

to delay it 3 seconds

avatar image antonychan0395 tormentoarmagedoom · Feb 05, 2020 at 05:23 PM 0
Share

thxx it's work

avatar image antonychan0395 tormentoarmagedoom · Feb 05, 2020 at 05:27 PM 0
Share

Also, when the plane touches the box, it just only shoots a group of ammo and that's all. However, I want the ammo will keep shooting in front of the plane. Plz help me.

void Update() { gameObject.transform.position += new Vector3(0, -0.02f, 0);
ammo2time += Time.deltaTime;

     if (hitpowerbox == true )
     {    

         
         
         Vector3 ammo2_pos = Ship.transform.position + new Vector3(2, 0.1f, 0);
         Instantiate(ammo2, ammo2_pos, Ship.transform.rotation);
         ammo2time = 0;
         

          }
         

     }

 

 void OnTriggerEnter2D(Collider2D col) 
 {
     if (col.tag == "plane") 
     {

         hitpowerbox = true;
         Object.Destroy(gameObject,0.1f);

     }
     
 }

}

avatar image ShadyProductions antonychan0395 · Feb 05, 2020 at 05:54 PM 0
Share

If you just do what I said you'd have your result :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bonfire-Boy · Feb 05, 2020 at 06:41 PM

As others have said, you're destroying the object as soon as it's hit, and before it has a chance to spawn the bullet.

So you want to move the Destroy call to after the ammo is spawned. I would do this in a coroutine, that makes it much easier to implement your delay, and keeps your Update function cleaner (ie it only contains things that happen on every frame).

I'm not 100% sure this gives you what you want as it's not clear what object your script is on or what "Ship" is, but it'd look something like this....

 Coroutine hitCoroutine;
 
 void IEnumerator HandleHit()
 {
     yield return WaitForSeconds(0.3f);
     Vector3 ammo2_pos = Ship.transform.position + new Vector3(2, 0.1f, 0); 
     Instantiate(ammo2, ammo2_pos, Ship.transform.rotation);
     Destroy(gameObject);
 }
 
 void OnTriggerEnter2D(Collider2D col) 
 {
     if (col.tag == "plane")
     {
         if (hitCoroutine == null) // this is to ensure that it only happens once
         {
             hitCoroutine = StartCoroutine(HandleHit());
         }
     }
 }
 
 void Update()
 { 
    gameObject.transform.position += new Vector3(0, -0.02f, 0);
 }
 
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

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how do i delete a stupid question? 1 Answer

Maya export to unity error 0 Answers

Attempt at re-arranging list results in crash 1 Answer

input axis gas pedal is not steup unity 0 Answers

Orbit problem in 5.0 unity 1 Answer


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