3

So basically I am trying to add a bunch of children to a GameObject. The parent GameObject is called SnakeHead while all the other GameObjects are called SnakeBodyParts

public class GameController : MonoBehaviour {
    public GameObject snakeHead;
    public GameObject snakeBodyPart;
    public List<GameObject> snakeBodyParts;

    void Start() {
        snakeBodyParts = new List<GameObject>();
        Instantiate<GameObject>(snakeHead);

        for (int i = 0; i < 4; i++)
        {
            var newObj = Instantiate<GameObject>(snakeBodyPart);
            newObj.transform.position = new Vector3(snakeHead.transform.position.x, snakeHead.transform.position.y - (i + 1), 0);
            newObj.transform.SetParent(snakeHead.transform);
            snakeBodyParts.Add(newObj);
        }
    }
}

So it looke like snakeHead.transform.childCount; is 1. Can anyone explain to me this kind of behaviour? Both SnakeHead and SnakeBodyPart are a simple Sphere added as a 3D Object to my project.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
user2877820
  • 287
  • 4
  • 19
  • When you run this in the debugger and pause, can you see the other children in the editor? – Sandy Gifford Sep 05 '17 at 17:16
  • 1
    can you show where you are calling for the `.childcount` – ryeMoss Sep 05 '17 at 17:17
  • @SandyGifford Besides SnakeHead I see 5 other GameObjects (original of SnakeBodyPart and 4 clones). So what is weird is that the original of snakeBodyParts are a child of SnakeHead, while all the clones I instantiate in this for-loop arent. – user2877820 Sep 05 '17 at 17:18
  • @ryemoss Right after the for-loop. – user2877820 Sep 05 '17 at 17:19
  • It looks like you're using snakeHead as an instantiable prefab, but never saving a reference to it after instantiating (`Instantiate(snakeHead);`). Do you set snakeHead in the editor with a prefab from your library? Or with an object in the game world? – Sandy Gifford Sep 05 '17 at 17:27

1 Answers1

3

I assume that snakeHead and snakeBodyPart are both prefabs.

The problem is that you are setting the parent of the snakeBodyPart with the snakeHead prefab instead of the instantiated snakeHead.

When you instantiate snakeHead, you are supposed to store it to a temporary variable the use it to set the parent of each instantiated snakeBodyPart(newObj).

Instantiate<GameObject>(snakeHead) should be GameObject snakeHaedObj = Instantiate<GameObject>(snakeHead); then you can do snakeHaedObj .transform.childCount; later on to check the count and set the parent of each snakeBodyPart with newObj.transform.SetParent(snakeHaedObj.transform);.

Something like this:

public GameObject snakeHead;
public GameObject snakeBodyPart;
public List<GameObject> snakeBodyParts;

GameObject snakeHaedObj;

void Start()
{
    snakeBodyParts = new List<GameObject>();
    snakeHaedObj = Instantiate<GameObject>(snakeHead);

    for (int i = 0; i < 4; i++)
    {
        var newObj = Instantiate<GameObject>(snakeBodyPart);
        newObj.transform.position = new Vector3(snakeHead.transform.position.x, snakeHead.transform.position.y - (i + 1), 0);
        newObj.transform.SetParent(snakeHaedObj.transform);
        snakeBodyParts.Add(newObj);
    }
}

Note that childCount will only return the direct child not but not Objects under the child. Take a look at this post for how to do that.

Programmer
  • 121,791
  • 22
  • 236
  • 328