Box colliders not in the same place as the game object (TEXT)

Hello!!! :slight_smile:

So I am new to Unity but I have a script in which I create a new letter every 3 seconds:

IEnumerator createLetters () {
        string letters = "abcdefghijklmnopqrstuvwxyz";

        while (true) {
            GameObject newLetter = new GameObject ();
            newLetter.AddComponent<BoxCollider2D> ();

            newLetter.tag = "letter";

            newLetter.AddComponent<GUIText> ().text = letters [Random.Range (0, letters.Length)].ToString ();

            x = Random.Range (0.3f, 0.71f);
            y = 1;
            pos = new Vector2 (x, y);

            newLetter.GetComponent<BoxCollider2D> ().offset = pos;

            newLetter.transform.position = pos;

            yield return new WaitForSeconds(3);
        }
    }

As you can see, I add a box collider to it. But this box collider does not appear in the same place as my letter (the GUItext) and I cannot see why not? I know the box collider-offset and the game object’s position is probably not the same thing, but I have tried multiple things and nothing is working the way I want it to.

Can anyone help me?

As you said, the collider offset is not the same thing as the Transform’s position, it’s an offset from the Transform’s position. If offset is anything other than (0, 0), the collider will not be centered on the GameObject.

If you want to add the collider and have it be in the same place as the text while the text may appear in slightly varying locations, then just remove line 16 from your script. I’m guessing you already tried this, though. If that isn’t what you wanted, what is it that you want?

1 Like

The problem is, that the box collider isn’t centered on the text(not even if I delete line 16) :-/ Can the GameObject be bigger than the text I applied to it?

Here is what I get…
I have copied the game objects from when the game was running and see these box colliders?

The box colliders should be in the same place as the text/letters.