- Home /
How to convert GameObject array to Transform array in C#?
Hello!
I'm developing a rhythm game with Unity. However, when I was modifying some scripts during development, I was having trouble specifying the type between arrays. I had to put the value of the GameObject array into an array that I had previously created as a Transform array.
Is there a way to convert a GameObject array to a Transform type, or to put the value that the GameObject array had into the Transform array?
I need help!!
I'm confused. Why can't you just declare the array like Transform[] myTransforms
?
Answer by landon912 · Jun 11, 2017 at 01:43 AM
Sure.
GameObject[] goArray = {...};
Transform[] transArray ;
transArray = new Transform[goArray.length];
for(int i = 0; i < goArray.length; i++)
{
transArray [i] = goArray[i].transform;
}
A little bit reversed but same logic with goArray[I].transform
Simplified with LINQ: Transform[] transArray = goArray.Select(f => f.transform).ToArray();
Answer by Enyph-Games · Jun 11, 2017 at 08:53 AM
GameObject[] goArray; Transform[] transformArray; transformArray.length = goArray.lenght; for(int i = 0; i < goArray.length; i++) { transformArray[i] = goArray[i].transform; }
Your answer
Follow this Question
Related Questions
Creating an array of prefabs? 4 Answers
Transform Checking on all Array Objects (JS) 3 Answers
Getting the transform from a GameObject list 3 Answers
C# Variables Transform vs GameObject 1 Answer
Keep adding targets to a list 2 Answers