Coin Instantiating in running game

In my running game pieces of the track are instantiated at start, and then moved around to make the endless track. When a piece is moved, coins should be instantiated again to the position it was moved to.
I have coin instantiate starting points, from which coins instantiate either on the x axis or the z axis with this script attached:

var parenttrack : Transform;
var parentpos : Vector3;

var dirx : boolean;  //if coins should instantiate on x
var dirz : boolean;  //if coins should instantiate on z

//x y z values of starting points
var x : float;
var y : float;
var z : float;

var enable : float;
var coin : Transform;

var numberOfCoinsInst : int;  //number of coins that will be instantiated
var numberOfCoinsInsted : int;  //number of coins that have been instantiated

function Start () {

enable = Random.Range(-1,2);  //if enable is 1 coins will instantiate from this starting point (giving 1/3 probability)
numberOfCoinsInst = Random.Range(5,15);


parenttrack = transform.root;
parentpos = parenttrack.transform.position;

}


function Update () {

x = transform.position.x;
y = transform.position.y;
z = transform.position.z;

if(enable == 1){

	if(parentpos == parenttrack.transform.position){
		InstantiateCoin ();
	}
	else{
		parentpos = parenttrack.transform.position;
		numberOfCoinsInsted = 0;
		enable = Random.Range(-1,2);
		numberOfCoinsInst = Random.Range(5,15);
	}
	
}


}



function InstantiateCoin () {

if(numberOfCoinsInsted < numberOfCoinsInst){
	if(dirx == true) {

		numberOfCoinsInsted ++;
		Instantiate (coin, Vector3 (x, y, z), Quaternion.identity);
		x += 0.03;
	
	}

	if(dirz == true) {
	
		numberOfCoinsInsted ++;
		Instantiate (coin, Vector3 (x, y, z), Quaternion.identity);
		z += 0.3;
	

	}

}

}

The coins are instantiating at start. The coins stop instantiating when the “numberOfCoinsInst” reaches “numberOfCoinsInsted”
But after the track moves, the coins arent instantiated again. The function InstantiateCoin is called, numberOfCoinsInst is made 0 again, which should make the coins instantiate again. but it does not work.

Apparently the change in position of the parent isnt working. when it changes parentpos is not becoming the new position. However in some of the coin starting scripts this IS working. which is very weird

I could not find the problem to this. Need help.

move

numberOfCoinsInst = Random.Range(5,15);
numberOfCoinsInsted = 0;

into

function InstantiateCoin () {
}

and call InstantiateCoin every time you move it.

Also, you will need to clear any un used coins from the track when you move it as well.

Unused coins are destroyed after some time. they dont get in the way, but i dont see why i should put those 2 lines in InstantiateCoin function. Because putting “numberOfCoinsInsted = 0;” will make coins keep instantiating. because it will never reach the limit. and there’s no point of putting numberOfCoinsInsted there either.

I have been able to make the coins inst after the track moves. however now the coins are all instantiating at one point. i have not looked into the script lately, ill make a post here when i do.

OK, the reason for my suggestion is so that whenever you call that function, it creates all of the coins you need.

It is to reset the random amount and counter back to zero before creating the coins.

Also, as I am looking at your code, you are also doing alot of work during the update that (in theory) doesn’t need to be done…

As I look at this though, you also are doing some weird things in your Update.

If it’s enabled… and it’s not in the parent position, move it to the parent position and reset the enabled?

So every time it “moves” it resets if it is enabled… and gives it’s self a 1 in 3 chance of setting that to true. If it is not true, then it never does anything else again.

Next… if parentpos equals parenttrack.transform.position… then create all of the coins… (ever update?) not good.

So my guess, is your code is simply giving a 1/3 chance of being active after the initial activity and that chance reduces every other frame. and if it happens to be active, it will throw 5-15 more coins out every frame.

So how to fix?

Limit your code where it will only generate coins when you move the track piece. (hence my comment earlier)

Another portion of this that is not quite right is the X and Z orientation of the coins. How do you turn from X to Z? do you have curved tracks? If so, does the curved track have coins on it?

First, here is the code distilled down to the working pieces:

var parenttrack : Transform;
var parentpos : Vector3;

var dirx : boolean;  //if coins should instantiate on x
var dirz : boolean;  //if coins should instantiate on z

var enable : boolean;
var coin : Transform;

var numberOfCoinsInst : int;  //number of coins that will be instantiated
var numberOfCoinsInsted : int;  //number of coins that have been instantiated

function Start () {
	RndEnable();
	parenttrack = transform.root;
	parentpos = parenttrack.transform.position;
	InstantiateCoin ();
}

//if enable is 1 coins will instantiate from this starting point (giving 1/3 probability)
function RndEnable(){
	enable = Random.value < 0.333;
}

function InstantiateCoin () {
	var numberOfCoinsInsted = 0;
	var numberOfCoinsInst = Random.Range(5,15);
	var pos = transform.position;
	if(numberOfCoinsInsted < numberOfCoinsInst){
		pos += dirx ? Vector3.right * 0.03 : Vector3.forward * 0.03;
		numberOfCoinsInsted ++;
		Instantiate (coin, pos, Quaternion.identity);
	}
}

I didn’t “fix” anything, I just took out the stuff that made no real sense. I also combined things as needed to make it all work.

So now, all you need to do is figure out what makes it move. And whatever makes it move is what needs to call InstantiateCoin();

okay, i didnt make my script and what i am trying to achieve clear. but i have my script working now.

#pragma strict

var parenttrack : Transform;
var parentpos : Vector3;

var dirx : boolean;
var dirz : boolean;
var x : float;
var y : float;
var z : float;

var enable : float;
var coin : Transform;

var numberOfCoinsInst : int;
var numberOfCoinsInsted : int;

function Start () {

enable = Random.Range(-1,2);
numberOfCoinsInst = Random.Range(5,15);


parenttrack = transform.root;
parentpos = parenttrack.transform.position;

}


function Update () {

x = transform.position.x;
y = transform.position.y;
z = transform.position.z;

if(enable == 1){

	if(parentpos == parenttrack.transform.position){
		InstantiateCoin ();
	}
	else if (parentpos != parenttrack.transform.position){
		parentpos = parenttrack.transform.position;
		numberOfCoinsInsted = 0;
		enable = Random.Range(-1,2);
		numberOfCoinsInst = Random.Range(5,15);
	}
	
}

}


function InstantiateCoin () {

if(numberOfCoinsInsted < numberOfCoinsInst){
	if(dirx == true) {
		x += 0.3;
		numberOfCoinsInsted ++;
		Instantiate (coin, Vector3 (x, y, z), Quaternion.identity);
	}

	if(dirz == true) {
		z += 0.3;
		numberOfCoinsInsted ++;
		Instantiate (coin, Vector3 (x, y, z), Quaternion.identity);
	}
}

}

im making parentpos = to the current transform of parent so that once they are equal, function InstantiateCoin is called. And the coins are instantiated either along the x axis (if dirx = true) or along the z axis (if dirz = true). This direction of instantiating is set manually.