- Home /
Find Gameobject Transform, dont work now?
I want:
private Transform[] waypoints;
void start()
{
waypoints = GameObject.FindGameObjectsWithTag("Waypoint").transform;
}
but dont work, only allow me to find Gameobjects. Any idea? thanks.
Answer by kieknai · May 30, 2015 at 11:23 AM
Well FindGameObjectsWithTag returns array of GameObject so you can't just call transform on it(since it does not return one GameObject but multiple), you can store the result in another array and then in a loop transfer it into your waypoints. Or just make them to be GameObject[] waypoints;.
Answer by FWCorey · May 29, 2015 at 09:46 AM
This will return an array object of GameObjects. Array doesn't have ".transform" as a property.
You can fix it by iterating over the returned array like this:
var newWaypoints = GameObject.FindGameObjectsWithTag("Waypoint");
if(newWaypoints != null && waypoints.Length > 0)
{
waypoints = new Transform[newWaypoints.Length];
for(int i = 0; i < newWaypoints.Length; i++)
{
waypoints[i] = newWaypoints[i].transform;
}
} else { waypoints = new Transform[0]; }
Your answer
Follow this Question
Related Questions
Finding class where current GameObject is assigned to as a var 1 Answer
transform.Find() returns a Transform? 1 Answer
Finding Children question 3 Answers
Finding object with transform 1 Answer
Find Transform in the scene 2 Answers