I have the following function:

public void Spawn ()

                GameObject g = ((GameObject)Instantiate (hills [Random.Range (0, hills.Length)], new Vector3 (0.0f, transform.position.y, transform.position.z), Quaternion.identity));
                g.transform.position = new Vector3 ((subs [subs.Count - 1].transform.position.x + (g.renderer.bounds.size.x) + (subs [subs.Count - 1].renderer.bounds.size.x) / 2), transform.position.y, transform.position.z);
                g.transform.parent = this.transform;
                NonLoopingScrollScript nls = (NonLoopingScrollScript)g.GetComponent ("NonLoopingScrollScript");
                nls.Setup (movSpeed, 0.0f);
                subs.Add (g);


Which is being used to spawn terrain, the way this works is I have an array(hills) which is composed of a bunch of prefabs which together will make up terrain in the background of the game. This function is what actually instantiates the next prefab when it is needed, this prefab is then added to a list (subs). My problem is simply the spacing between the prefabs as I would like there to be no space, yet sometimes the objects overlap or there is a gap between the prefabs.

Explanation of code: In the first line I instantiate a random prefab with the argument:

hills [Random.Range (0, hills.Length)]

The next argument is where I am having trouble, what I am currently doing is setting the x value of the new prefab to the x value of the transform plus half the x width of both the previous prefab, and the new prefab. This is the issue as the spacing is not as desired. Then i simply set the y and z value to that of the spawner object and the rest of the code is irrelevent, save for where at the end of the function I then add the new gameobject to the end of the list.

I hope this was clear, if not please do not hesitate to ask questions.

Edit: The seperation of the prefabs works fine when there is only one type of prefab, but when I have several it doesn't. Here is an image of what I mean: The selected stuff is what you should be looking at


1 Answer 1


I’ve been using tk2d so I might be missing something crucial, but it seems like you want:

g.transform.position = new Vector3 ((subs[subs.Count-1].renderer.bounds.max.x + g.renderer.bounds.extents.x),transform.position.y,transform.position.z);

subs[subs.Count-1].renderer.bounds.max.x should get you the right edge of the previous hill sprite, and g.renderer.bounds.extents.x will get you an offset half the width of the new hill sprite.

This assumes that your sprite prefabs are centered over the transform position. If they’re not you'll need to adjust the offset part. E.g., if the g.transform.position.x represents the left of the sprite, then you could just set the Vector3's x to subs[subs.Count-1].renderer.bounds.max.x

  • \$\begingroup\$ May I ask what the difference between g.renderer.bounds.size.x, g.renderer.bounds.extents.x, and .renderer.bounds.max.x. I haven't seen the other two before. \$\endgroup\$ Commented Feb 15, 2014 at 5:48
  • \$\begingroup\$ Glad it helped.They're each Vector3s. docs.unity3d.com/Documentation/ScriptReference/Bounds.html covers what they mean/how they're calculated. \$\endgroup\$ Commented Feb 15, 2014 at 5:58

You must log in to answer this question.

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