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.