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 knightmar · Feb 26, 2019 at 09:20 PM · triggertaggameobjectsfor

Do something only "for" Gameobjects in trigger

Hey, I want to do something few times (in for) but only for gameobjects in trigger. (I'm using tag to determine gameobjects but I can't use OnTriggerEnter etc.) If something in this for will make gameobject leave the trigger i don't want it to participate in the next for. What should I use? ///// P.S. I don't need to use trigger if there's something better but idea is to randomly pick gameobject from the gameobject[] and teleport it somewhere in 1st "for" and don't use it in next "for" so it'll not be picked again.

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
0

Answer by AaronBacon · Feb 26, 2019 at 10:06 PM

So If I'm understanding this right you want to keep track of every GameObject that is touching a trigger, then pick a random item from this list of items, and then delete it from the list.

Your question isn't exactly worded clearly but assuming that's it, this is what I would do:


I'm not sure why you can't use OnTriggerEnter, it's the simplest way to tell if one thing is in the collision of another, so I would make something like this:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObjList : MonoBehaviour
 {
     public static List<GameObject> objects; // stores the objects currently in contact with the trigger
 
     private void OnTriggerEnter(Collider2D collision)
     {
         if (collision.gameObject.CompareTag("Object")) // If the object is tagged as an object
         {
             objects.Add(collision.gameObject); // Add the Object to the List
         }
     }
 
 
     private void OnTriggerExit(Collider2D collision)
     {
         if (collision.gameObject.CompareTag("Object")) // If the object is tagged as an object
         {
             objects.Remove(collision.gameObject); // Remove the Object from the List
         }
     }
 }

That should track objects tagged "Object" currently touching the object and keep them in a list called objects. Then to select a random object from that list and remove it, write a function like this:

 private GameObject PickRandom()
 {
     System.Random rnd = new System.Random();
     int index = rnd.Next(0, (objects.Count)-1);
     return objects[index];
     }
 

And now you can use PickRandom() to pick a random GameObject from the ones currently touching the GameObject this script is on. From there you can teleport that Object or co whatever you need to iy

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

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

Do a collider only with certain taged objects 2 Answers

Use trigger with player but have a collider with everything else? 3 Answers

Is it possible to destroy a gameobject from a child gameobject's script? 1 Answer

Help with collide triggers and calls, please. 1 Answer

Collision by tag issues 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