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 /
  • Help Room /
avatar image
0
Question by Centrixed · Jan 22, 2016 at 01:35 AM · scripting problemobjectslearningobject reference

Trying to track the BoxColliders of 2 Different Sets of Instantiated GameObjects

Basically I have dynamically spawning (instantiated) "enemies" that will each have their own BoxCollider2D's. I also have instantiated projectiles coming from my player with their own unique BoxCollider2D's as well. I have been trying to think of a way to dynamically track these colliders, since they have no unique properties to track individually. Is it possible to group them, but still have access to an individual collider? For example: "if a collider from the group of projectile boxcolliders touches a collider from the group of enemy colliders, damage that specific enemy". I am still learning the in's and out's of Unity Engine, so I may just be overlooking a tool they already provided. Can someone please give me insight on how to accomplish this, or at least where to start? Thanks! (Also if this code has a horrible flaw, please point it out so I can fix my mistake)

Here is my code for the projectile (animations are handled in another script):

 public class Projectile : MonoBehaviour {
 
     public GameObject projectilePrefab;
     public float speed = 0f;
     private Rigidbody2D playerRigid;
     private Rigidbody2D cloneRigid;
     private GameObject clone;
     private MovementScript playerMove;
     private Animator animate;
     private Vector2 projectileSpeed;
     private bool shot = false;
 
     void Start () {
         playerRigid = GetComponent<Rigidbody2D>();
         playerMove = GetComponent<MovementScript>();
         animate = GetComponent<Animator>();
     }
     
     void Update () {
         //If Player is facing right, adjust projectile direction on x axis.
         if (playerMove.faceDirection) {
             projectileSpeed = new Vector2(1 * speed, 0);
         }
         //If Player is facing left, reverse projectile direction on x axis.
         else if (!playerMove.faceDirection){
             projectileSpeed = new Vector2(-1 * speed, 0);
         }
 
         //Calls the isThrowing bool from another script, which mediates cooldown time between throws.
         if (playerMove.isThrowing) {
             if (!shot) {
                 shoot();
                 shot = true;
             }
         }
         else if (!playerMove.isThrowing) {
             shot = false;
         }
     }
 
     //Instantiates the object using the projectile prefab, then rotates/scales it accordingly and projects it outwards.
     void shoot() {
         //Instantiates the projectile, and gets components of the clone to work with.
         clone = (GameObject)Instantiate(projectilePrefab, playerRigid.position, Quaternion.identity);
         cloneRigid = clone.GetComponent<Rigidbody2D>();
         Transform cloneTrans = clone.GetComponent<Transform>();
 
         //Rotates the sprite so it visually flies in the correct direction
         if (playerMove.faceDirection) {
             cloneTrans.Rotate(0, 0, 270);
         }
         else {
             cloneTrans.Rotate(0, 0, 90);
         }
         //Scales the sprite to a feasable size
         cloneTrans.localScale = new Vector3(15, 13, 1);
 
         //Applies velocity and destroy clone after 3.5 seconds
         cloneRigid.velocity = projectileSpeed;
         Destroy(clone, 3.5f);
     }
 }
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

1 Reply

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

Answer by ZefanS · Jan 22, 2016 at 08:50 AM

It sounds to me like Tags would provide the functionality you're looking for. Tags can be used to group GameObjects and identify them. They can, for instance, be used to identify what general type of object something collided with. Here's a quick example (this script would be attached to the projectile gameobject):

 #pragma strict
 
 function OnCollisionEnter(collision : Collision)
 {
     //If the gameobject we hit is tagged "Enemy", destroy it
     if (collision.gameObject.tag == "Enemy")
     {
         GameObject.Destroy(collision.gameObject);
     }
 }

Hope this helps!

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

41 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

Related Questions

items not adding to list 1 Answer

Trouble with creating objects 1 Answer

How do I set value ScriptableObject list element property in script? 1 Answer

Object Reference error when no object is being referenced. 0 Answers

How can I move and to a random position?,How can I move an object every time I saw it? 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