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
0
Question by antonychan0395 · Feb 04, 2020 at 03:02 PM · answers

Why the value of caseswitch can't change ?

when I touch the gameobject, the switch value cannot change. caseswitch = 0

 // Update is called once per frame
 void Update()
 {
     gameObject.transform.position += new Vector3(0, -0.02f, 0);
     ammo2time += Time.deltaTime;
     
     switch (caseswitch)
     {
         case 0:
             break;


         case 1:
             if (ammo2time > 0.30f)
             {

                 Vector3 Bullet_pos = Ship.transform.position + new Vector3(2, 0.1f, 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;
     }

 }

Comment
Add comment · Show 4
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 04, 2020 at 03:25 PM 0
Share

Can you please also include the full line where you declare caseswitch first? (You may also want to give it a more descriptive name. What does it represent and what do you intend to do with it?)

avatar image antonychan0395 JPhilipp · Feb 04, 2020 at 03:35 PM 0
Share

the default value of caseswitch is 0 when my plane touch the power logo, the value will change to 1

avatar image JPhilipp antonychan0395 · Feb 04, 2020 at 03:47 PM 0
Share

In that case, you would make it a bool (true/ false), and name it e.g. touchedPowerLogo. This way, you will be able to more easily maintain your program. You then do a check for if (touchedPowerLogo) ins$$anonymous$$d of the case-check. In either case, to continue debugging your issue, we would need the full line where you declare that variable.

avatar image antonychan0395 JPhilipp · Feb 04, 2020 at 03:35 PM 0
Share

so the ammo will spawn in front of the plane

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by INvalidSauce · Feb 04, 2020 at 03:57 PM

You're destroying the gameobject before you try and change your variable caseswitch. When you destroy the gameobject that holds the script, it can no longer run. You need to do the destroy function AFTER "casesswitch = 1"

 void OnTriggerEnter2D(Collider2D col) 
  {
      if (col.tag == "plane") 
      {
          caseswitch = 1;
 
          Destroy(gameObject);
      }
  }

EDIT: Reading a bit more into it, I don't quite get why you're destroying your gameobject. If you destroy it, you'll never get to use the variable you just changed. The switch statement in your update() won't ever do anything.

Comment
Add comment · Show 3 · 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 Bunny83 · Feb 04, 2020 at 04:12 PM 0
Share

Your statement is pretty irrelevant. It doesn't really matter if you call Destroy before or after you change the variable. Destroy is delayed until the end of the current frame anyways.


Your edit is the actual answer. It's just pointless to change anything on that gameobject or its attached components if you destroy it.

avatar image antonychan0395 Bunny83 · Feb 04, 2020 at 04:45 PM 0
Share

and if the plane touches the box, the ammo will not follow the movement of the plane.

avatar image antonychan0395 · Feb 04, 2020 at 04:48 PM 0
Share

If I delete destroy(gameobject), it will run properly. But I want to delete the object when my plane touch it. It is because when the plane touches the box, it will give the plane buff and shoot more ammo.

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

121 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 avatar image

Related Questions

What Is/Are... (Common Game Terms) 1 Answer

Problem with unity answers? 1 Answer

UDN Crashes On New Account 0 Answers

Latest SDK not working 0 Answers

[META] Answer is sending notification only globally? 0 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