0
\$\begingroup\$

I have an array of Transforms:

public Transform[] monsterOne; 

I can put a child's transform in this array using this code:

monsterOne = child.GetComponents<Transform>();

But if I try to do this in a loop for each child, the next child just replaces the last one, instead of adding to the array:

foreach (Transform child in transform)
{
    monsterOne = child.GetComponents<Transform>();
}

How can I add all of these transforms into one collection?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ It's very unclear what you're trying to accomplish here. I'll ask you again: please edit your question to describe what game feature you're trying to make. It looks like there are probably better ways to do this than the code you've shown us, but if we can only see the code - not why you're trying to do this at all - then it's hard for us to help. \$\endgroup\$
    – DMGregory
    Aug 7, 2019 at 17:15
  • \$\begingroup\$ Well, I have a GameObject grave in the scene, and this grave is supposed add a monster prefap every 15 seconds, I want to add a limit to monsters number, so i have tried to put the monsters in the 'list' and count many are in the list, so if there are 4 monsters stop adding prefap monsters \$\endgroup\$ Aug 7, 2019 at 17:35
  • \$\begingroup\$ Then it's much better to ask: "How can I limit the number of monsters my spawner creates?" — ie. ask about what you want to accomplish, not about the code you think might accomplish it. This helps you avoid the X/Y Problem and get better answers than the code you've thought of so far. In this case, you might not need a list at all — you could ask your monsters to count themselves \$\endgroup\$
    – DMGregory
    Aug 7, 2019 at 17:41
  • \$\begingroup\$ Thank you it works But I'm still curious about my way the X/Y But thank you \$\endgroup\$ Aug 7, 2019 at 17:57

2 Answers 2

1
\$\begingroup\$

Arrays in C# can't be appended-onto. What you might want instead is a List, which behaves a lot like an array, but you can add items to it:

// Declaration of member variable - you can initialize it at the same time.
public List<Transform> children = new List<Transform>();

...

// Populate the list's contents in a method like Start().
foreach(Transform child in this.transform)
{
    children.Add(child);
}

You can still access the ith item in the collection with children[i] or iterate over everything in the collection with foreach. The main difference is that if you want to know how many entries are in the collection, you need to use .Count with Lists, as opposed to .Length with Arrays.

Alternatively, you can pre-size the array to the size you need, then assign each entry one by one:

// Declaration of member variable - don't initialize it yet (we don't know the size yet).
public Transform[] children;

...

// Allocate an array of the right size and populate it inside a method like Start().    
int childCount = transform.childCount;
children = new Transform[childCount];

for(int i = 0; i < childCount; i++)
{
    children[i] = transform.GetChild(i);
}
\$\endgroup\$
0
\$\begingroup\$

Every game object can have only one Transform compinent. You are trying to get multiple Transform components from one transform. Instead you should loop through parent's children. If monsterOne is your Transform of the parent.

Example:

for(int i = 0; i < monsterOne.childCount; i++) {
    monsterOne.GetChild(i); //This is the transform of each child in monsterOne 
}
\$\endgroup\$
3
  • \$\begingroup\$ i got eroor "childCount" and "GetChild" \$\endgroup\$ Aug 7, 2019 at 17:12
  • \$\begingroup\$ 'Transform[]' does not contain a definition for 'childCount' and no accessible extension method 'childCount' accepting a first argument of type 'Transform[]' could be found (are you missing a using directive or an assembly reference?) \$\endgroup\$ Aug 7, 2019 at 17:12
  • \$\begingroup\$ Because you are supposed to call those on the parent's Transform. Not on an areay of transforms. In that example monsterOne was the transform of the parent. \$\endgroup\$
    – Cantica
    Aug 7, 2019 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .