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.