- Home /
Question by
Shipendo · Dec 01, 2019 at 09:14 PM ·
scripting problem
Add collided gameobjects to an array
Hi! I have recently run into a problem in my code and could use some help. Is there an easy way to add all the game objects I collided with to an array? Specifically, all the game objects with a certain tag. I can't find an easy way to do this.
Thankful for any and all help.
Comment
Answer by Cornelis-de-Jager · Dec 01, 2019 at 09:22 PM
here you go
List<GameObject> collidedObj;
void Start () => collidedObj = new List<GameObject>();
void OnCollisionEnter (Collider col) =>
if (col.transform.tag == "Mytag" && !collidedObj.Contains(col.gameObject))
collidedObj.Add(col.gameObject);
Your answer
Follow this Question
Related Questions
How can i create array of texture2d with variables names ? 2 Answers
Casting a GraphView node using GetNodeByGuid() returns null 1 Answer
Is there any way to link an image object to another game object's component? 0 Answers
2D gun shooting and fliping 1 Answer
what is the new Get Tag From Raycast? 0 Answers