Question by
vonbetelgeuse · Jul 15, 2016 at 03:55 PM ·
transformgetcomponentarraysgameobjectsfindgameobjectswithtag
c# Getting Transforms from array made from FindGameObjectsWithTag
Hey guys!
Short question,
I've made an Array of GameObjects using FindGameObjectWithTag, and now I want to take the transforms off those GameObjects and put them in a Transform Array, how should I formulate this?
GameObject[] taggedWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");
Transform[] waypoints = taggedWaypoints[].GetComponent<Transform>; // this is definitely wrong, how do I correct this?
Comment
Best Answer
Answer by TBruce · Jul 15, 2016 at 04:26 PM
You can do it like this
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
GameObject[] taggedWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");
List<Transform> waypoints = new List<Transform>();
for (i = 0; i < taggedWaypoints.Length; i++)
{
waypoints.Add(taggedWaypoints[i].GetComponent<Transform>());
}
Excellent! You're my personal saviour, thank you! :)
Your answer
Follow this Question
Related Questions
How to return a value from an array of GameObjects? 1 Answer
this.GetComponent() won't return gameObject? How to get it without using this.gameObject? 3 Answers
Moving spawned enemies randomly towards several pre-defined positions 1 Answer
How would I access a property of another objects child component? 2 Answers
Problem with reading values from scripts on GameObjects in Lists 1 Answer