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 halfdevil333 · Aug 10, 2016 at 03:12 PM · gameobjectarrayif-statementstags||

Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code.

i have a few more if statements just like this one obviously changing the tags and player number each time. also is there a more efficient way of doing this. thanks

 if (player[0] && (col.gameObject.tag == "2" || col.gameObject.tag == "3" || col.gameObject.tag == "4")) 
 { 
 Destroy (gameObject); 
 } 
 if (player[0] && col.gameObject.tag == "1") 
 { 
 Destroy(col.gameObject); 
 }
Comment
Add comment · Show 3
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 halfdevil333 · Aug 10, 2016 at 04:28 PM 0
Share

i want a square[0] to be destroyed by triangles(tag = 2) and circles(tag = 3) when the square comes into contact with them. also i want square[0] to destroy square(tag = 1) and not be destroyed by its self. the code bellow destroys both square[0] and any other object it comes in contact with. also if there is a more efficient way to put this please tell me. as on actually script i am dealing with 9 tagged objects and 9 array objects. thanks

avatar image NoseKills halfdevil333 · Aug 10, 2016 at 06:32 PM 0
Share

What do you mean to check with if (player[0] && col.gameObject.tag == "1")? Did you mean to check player's tag too. Now it jsut checks if player[0] is not null.

$$anonymous$$ight be possible to write it simpler but not with so little information. What is the general logic of how to destroy things? If you hit a shape that's the same as you, both get destroyed, otherwise just you?

avatar image halfdevil333 NoseKills · Aug 10, 2016 at 07:03 PM 0
Share

if you(hero) hit same shape other shape(enemy) gets destroyed, if you hit different enemy, hero gets destroyed.

i have tagged the different shaped enemies differently. also the hero keeps changing shapes thats why heroes are in a player array

2 Replies

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

Answer by halfdevil333 · Aug 11, 2016 at 08:08 AM

50 lines of code cut down to this, thank you @NoseKills for placing the idea into my head.

  void OnCollisionEnter2D (Collision2D col) {
 
         if (gameObject.tag == col.gameObject.tag) {
             Destroy(col.gameObject);
         }
         else { Destroy(gameObject); }        
     }

 

you dont know how happy i am. thanks man

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
2

Answer by NoseKills · Aug 10, 2016 at 08:10 PM

Assuming player[] array is an array of Unity Objects, right now your code checks

 if (player[0] && (col.gameObject.tag == "2" || col.gameObject.tag == "3" || col.gameObject.tag == "4")) 

if the object in player[0] is not destroyed/null, and the colliding object has tag 2, 3, or 4 => destroy this gameobject.

after that you do

 if (player[0] && col.gameObject.tag == "1") 

if the object in player[0] is not destroyed/null, and the colliding object has tag 1 => destroy the other gameobject.

There's no else connecting the two ifs so you always do both checks.

if you(hero) hit same shape other shape(enemy) gets destroyed, if you hit different enemy, hero gets destroyed.

You can write this system more simply. Your are checking shapes so it would be a good starting point to make an object you can use to describe shapes. For example

 public class Shape : MonoBehaviour {
     public enum ShapeType {
         Square,
         Circle,
         Triangle,
         Pentagon,
     }
 
     public ShapeType type;
 }

Attach that to all prefabs that need to have a shape and use the dropdown in the inspector to set the desired shape to all of them.

After that you can write the condition code almost like written text.

 var playerShape = player[0].gameObject.GetComponent<Shape>();
 var otherShape = col.gameObject.GetComponent<Shape>();
 
 if (otherShape != null && playerShape != null) {
     if (myShape == otherShape) { // if player and colliding shape are the same
         Destroy(otherShape.gameObject);
     } else {
         Destroy(gameObject);
     }
 } else { 
     // this is just to check whether you remembered to attach Shape scripts 
     if (otherShape == null) {
         Debug.LogError(col.gameObject.name + " did not have a 'Shape' Component");
     } else {
         Debug.LogError(player[0].gameObject.name + " did not have a 'Shape' Component");
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Need a Custom array Index to look up gameobject tags? 2 Answers

Find and store gameObjects in a array 2 Answers

Hi, getting error cs1041 in my code:identifier expected, "this" is a keyword; need solution please 3 Answers

Unload from memory - GameObject in an array? 1 Answer

How to set to game objects's position from 2 different game objects arrays equal to each other? 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