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 Dogg · Aug 27, 2014 at 06:10 AM · gameobjectdestroyscore

How To Stop Destroyer From Destroying?

Hey I have a question, how can I stop the destroyer(it's a big quad that destroys all leftover gameobjects) from destroying certain game objects? You see i have power ups in the game that give me extra points, and every time the destroyer destroys them I get points for it, which of course i don't want to happen. So how can stop the destroyer from destroying the power ups? Or how can I stop the power up from adding scores when destroyed by the destroyer? Here's my destroyer script and power up script so you know what I'm talking about:

 public class DestroyerScript : MonoBehaviour {
     
     HUDScript hud;
     
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player") 
         {
             Application.LoadLevel(1);
             return;
         }
 
         if (other.gameObject.transform.parent) 
         {
             Destroy (other.gameObject.transform.parent.gameObject);
         } 
         else 
         {
             Destroy (other.gameObject);        
         }
     }
     
 }
 

Powerup:

 public class Powerup : MonoBehaviour {
 
     ScoringScript hud;
     
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         Destroy (gameObject);
         {
             hud = GameObject.Find ("Character").GetComponent<ScoringScript> ();
             hud.IncreaseScore (3);
         }
     }
     
 }
 

And the script that calculates my score:

 public class ScoringScript : MonoBehaviour {
     
     float playerScore = 0;
     bool isPaused = false;
     public bool scoring = true;
     public float guiPlacementY1;
     public float guiPlacementX1;
     PlayerScript player;
     
     void Start()
     {
         player = GetComponent<PlayerScript >();
         scoring = true;
     }
     
     
     public void IncreaseScore(int amount)
     {
         playerScore += amount;
     }
     
     public void DecreaseScore(int amount)
     {
         playerScore -= amount;
     }
     
     
     void OnDisable()
     {
         PlayerPrefs.SetInt ("Score", (int)(playerScore * 5));
         PlayerPrefs.SetString("PlayerName", "High Score");
         PlayerPrefs.Save ();
         Debug.Log("Hide");
     }
     
     void OnGUI()
     {
         
         GUI.Label (new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .4f), "Score: " + (int)(playerScore * 5));
     }
 }
 
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 AgeTDev · Aug 27, 2014 at 07:19 AM

If I understand you correctly, you dont want your destroyer to destroy the powerups when they collide, right ? So , I see you already use a tag to check whether the Player is colliding with the destroyer or something else, so what you could do is just use another if-condition to see if the "other" object is actually a powerup.

Lets say you put a tag on them called powerup,then you could add this line :

 if (other.tag != "powerup")
 {
   //Your existing code here
   /*
      if (other.gameObject.transform.parent) 
         {
             Destroy(other.gameObject.transform.parent.gameObject);
         } 
         else 
         {
            Destroy (other.gameObject);        
         }
    */
 }


Also , i would add an else-statement after the if that checks if your Player is hit.

Hope i can help, cheers, T

Edit : and ofcourse what the Person before me suggested , namely the Destroy call in your powerup Needs to be inside the brackets.

Comment
Add comment · Show 9 · 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 Dogg · Aug 27, 2014 at 07:53 AM 0
Share

Let me make sure i got this right. So like this?:

 void OnTriggerEnter2D(Collider2D other)
     {
 
         if (other.tag == "Powerup")
         {
 
         if (other.gameObject.transform.parent) 
         {
             Destroy (other.gameObject.transform.parent.gameObject);
         } 
         else 
         {
             Destroy (other.gameObject);        
         }
     }
         else
             if(other.tag == "Player")
         {
             Application.LoadLevel(1);
             return;
     
   }
     }
avatar image AgeTDev · Aug 27, 2014 at 08:46 AM 0
Share

I'd say the other way around, first check if it IS the Player , and then check if IT IS NOT the power up , because if it is the power up , you do not want to do anything with it , right ?

so with your code :

 void OnTriggerEnter2D(Collider2D other)
     {
 
             if(other.tag == "Player")
         {
             Application.LoadLevel(1);
             return;
  
         }
        else 
        {
         if (other.tag != "Powerup")
         {
  
          if (other.gameObject.transform.parent) 
          {
             Destroy (other.gameObject.transform.parent.gameObject);
          } 
          else 
          {
             Destroy (other.gameObject);        
          }
         }
        }  
     }



The important part is the " != powerup " because then it will only perform your Actions if the tag is something else. and because you checked if it is the Player before, this shoudl be what you were asking for :)

hope it works, T

avatar image Dogg · Aug 27, 2014 at 08:55 AM 0
Share

Sadly it's not working. Isn't there just a way to tell the destroyer script to completely ignore the power up? I'm creating a short level, so it shouldn't affect the performance by that much.

avatar image AgeTDev · Aug 27, 2014 at 09:14 AM 0
Share

Thats what this should Do, Are you Sure you did add the tag to the PowerPCs ?

avatar image Dogg · Aug 27, 2014 at 09:19 AM 0
Share

Yep, I added the tag to the power up prefab and the game object/spawn. It must be something else that's causing this to not work. I'll have to look into it more.

Show more comments
avatar image
0

Answer by JustFun · Aug 27, 2014 at 06:35 AM

You have a mistake in powerup script:

        if (other.tag == "Player") Destroy (gameObject);
        {
            hud = GameObject.Find ("Character").GetComponent<ScoringScript> ();
            hud.IncreaseScore (3);
        }

With such placed braces, IncreaseScore() is called always, regardless of colliding objects. You need to move Destroy() into braces:

        if (other.tag == "Player")
        {             
            hud = GameObject.Find ("Character").GetComponent<ScoringScript> ();
            hud.IncreaseScore (3);
            Destroy (gameObject);  
        }
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 Dogg · Aug 27, 2014 at 07:52 AM 0
Share

I see it appears I missed that. I just changed it, it didn't affect anything. The power ups still increase my score when they get destroyed by the destroyer. Thanks though for trying to help me. I appreciate it. :)

avatar image JustFun · Aug 27, 2014 at 08:06 AM 0
Share

Is powerup script the only place, where IncreaseScore() function is called?

avatar image Dogg · Aug 27, 2014 at 08:14 AM 0
Share

No. I have other scripts but those are disabled. Other than that, there is no other place where IncreaseScore is called.

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

23 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

Related Questions

Multiple Cars not working 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Why does this do nothing (Health Script) 2 Answers

Hit Collision with exceptions 2 Answers

How to Destroy a gameobject on collision 3 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