Help with Generating/Spawning Background Object

Hi

Up to this point, I’ve been generating an object with texture and using that as my background image. I start off with two objects, 1.33 wide by 2.66 tall (each). These have also been made into prefabs called “Platform.” They’re called platform but they’re really background objects. They are stacked vertically and scroll downwards. When the first object’s top edge reaches the end of the camera view, it generates a new background object, above the second object (that was there at the beginning) so that it creates a continuous scrolling background (as far as the camera can tell). This works great if you’re only using one prefab with the same texture. The texture is a top down view of some landscape. But now, I want to be able to generate a changing landscape. I want to randomly determine if the landscape will transition and if so, I want to spawn another prefab with a different landscape texture. So to start, I created a separate background object with a different texture.

Before this, I was using transform.position = new Vector 3( ) to generate the new prefab but since now I want to access other prefabs in the assets folder, I’m trying to use “Instantiate( )”. When I use new Vector 3( ), the prefabs generate only when the condition is met and works great. But now, when I use “Instantiate ( )”, the prefab is generated almost back to back, instead of only when the prefab (self) reaches the final position.

Maybe I’m missing something about “Instantiate ( )” that I don’t understand or not sure what the solution for this problem is here. Any help would be great…

Oh I think I see. You need to destroy the game object that creates the new prefab instance. Otherwise, that game object will continue to move down the screen, its position will continue to be < -1.55, and it will keep spawning new ones. Just add a Destroy(gameObject) right after you instantiate the new one.

Hi Supernat, thanks for trying to look into this.

Regarding transform.position = new Vector3( ), you are correct. I was initially using this to make the background object change it’s position, once it reached a certain final position. This was implemented only on one default background object.

Now, what I want to do is, dynamically select from different prefabs (same cube object with same dimensions each, only difference is that each object will have a different texture), spawn the prefab in the position desired, and it will scroll from top to bottom.

As far as scrolling, I’m using the Playmaker Plugin. The object has a Playmaker component attached that has a Finite State Machine. Once the object is spawned, it scrolls the object from top to bottom. So, we don’t need to worry about the scrolling portion of this, that works the way it’s supposed to, even when I use Instantiate ( ). The problem with Instantiate is that somehow, instead of doing the action when the object (self) is at the terminal position, it’s spawning repeatedly.

The script attached to the object does a couple of things: (1) checks the current position of the objet it’s attached to (2) once object reaches terminal position, repositions self at the new desired position (using new Vector 3 ( ) ).

I want to change the script around to where it (1) checks the position (2) spawns a prefab (with disctinct texture attached) saved in the assets/prefabs folder. Here is the code I was using up to now, this is the one that simply takes the default background and repositions it using new Vector 3 ( ):

#pragma strict
var speed : float;
var position_y : float;

function Update () {

     //The game starts off with 2 tiles that are 2.66 units long each (5.32 units total)
     //The new position of the first background object (position_y) = (2.66x2)-1.55=3.77 units
     //This ensures the background object is placed right above (edge to edge) the second object
     if (transform.position.y <= -1.55) {
	     position_y = 3.77f 
	     transform.position = new Vector3(0, position_y, 1);
     }

}

When using a single background object this works fine. But now, I want to create several prefabs. I’m only using two prefabs right now to test with until I can get this down then I will create more prefabs. But I want the script attached to the background object to (1) check it’s current position (2) if at the terminal position then, spawn one of two prefabs in the prefabs folder at the desired position. The prefab will already have the scroll FSM attached and will scroll once spawned. (3) Don’t do anything except scroll until it reaches terminal position then, spawn another prefab at the desired position.

Here is the script I’m implementing. Again, keep in mind that scrolling works fine. The problem I’m having is that with this script, once the object instantiates, seems to be instantiating prefabs one after the other (one clone each frame or so) even as each one scrolls down the way it’s supposed to. Here’s the code I’m using:

function Update () {
     if (transform.position.y <= -1.55) {
     position_y = 3.77f;
     transition_Bool = (Random.value > 0.5f);
     //Each if statement will set the Prefab Path (string) for the specific prefab to be spawned
    		if (transition_Bool == true) {
      		Prefab_Path = "Assets/CityStreets/Prefabs/Platform_Bridge.prefab";
       		}
       		else if (transition_Bool  == false) {
       		Prefab_Path = "Assets/CityStreets/Prefabs/Plaform.prefab";
       		}
       		else { 
       		}
    		Instantiate(AssetDatabase.LoadAssetAtPath(Prefab_Path, GameObject), Vector3(0, position_y, 1), Quaternion.identity);
    	}
}

This is the same basic structure as the first code section. Only difference is the if statement for setting the prefab path, but that only sets the prefab path for the Instantiate function to use. The only other difference here is that instead of using new Vector 3, I’m using Instantiate ( ).

Not sure what it is about this second script that’s causing the prefab to spawn what seems to be every frame, instead of only once, after the lower edge of the last object reaches the terminal position.

Any help will be great.