Dinu Adrian
Dinu Adrian

Reputation: 139

Why doesn't the function get called the second time?

I am trying to spawn some GameObjects in game based on the current player position, essentially trying to make an infinite runner type of game...I made a function so I can spawn the pylons with it but for some reason the function gets called only once per frame, It does not get called the second time with a different parameter.

Why does the second call of the function not work?

This is my code:

public class CameraScript : MonoBehaviour {
    public float cameraSpeed = 1;
    public float horizontalSpeed;
    private int spawnIndex;
    public float spawnNormPylonDis;
    public float spawnCoinPylonDis;
    private int currPosition ;
    public GameObject[] pilons;
    public GameObject spawnMainPoint;
    public Transform[] spawnPoints;
    public Transform[] coinsSpawnPoint;
    public float enamySpeed;
    private int currentPoint;
    public Transform[] pointPosition;

    // Use this for initialization
    void Start () {
        //This spawns the Pilons.
        spawnMainPoint.transform.position = pointPosition [0].position;
        currPosition = (int) transform.position.z;
    }

    // Update is called once per frame
    void FixedUpdate () {

    spawnMainPoint.transform.position = Vector3.MoveTowards (spawnMainPoint.transform.position, pointPosition[currentPoint].position, Time.deltaTime * horizontalSpeed);

    SpawnPylon (pilons[1],spawnPoints,spawnNormPylonDis,"Check function");
    SpawnPylon (pilons [0], spawnPoints, spawnCoinPylonDis,"Check the second function");

    GetComponent<Rigidbody> ().velocity = transform.forward * cameraSpeed;

    //the next if statements make the Pilons spawn randomly and corectly.
    if (spawnMainPoint.transform.position == pointPosition [currentPoint].position) {
        currentPoint++;
    }
    if (currentPoint == pointPosition.Length) {
        currentPoint = 0;
    }

}
/*This function spanws the a GameObject randomly at a GameObject's position and it takes 2 arguments :
Argument 1: type GameObject 
         2: type Transform[]*/
void SpawnPylon (GameObject whatSpawn,Transform[] whereSpawn,float spawnDistance,string plm)
{       
    bool hasSpawnedPylon = false;

    if (currPosition != (int)transform.position.z)
    {
        if ((int)transform.position.z % spawnDistance == 0) 
        {
            Debug.Log (plm);
            if (!hasSpawnedPylon) 
            {
                //this makes the GameObject spawn randomly
                spawnIndex = Random.Range (0, spawnPoints.Length);
                //This is instantiationg the GameObject
                Instantiate (whatSpawn, whereSpawn [spawnIndex].position, whereSpawn [spawnIndex].rotation);
                //this makes shore that the GameObject is not spawned multiple times at aproximetley the same position.
                currPosition = (int)transform.position.z;
            }
        }
    }
    else
    {
        hasSpawnedPylon = false;
    }           
}
}

Here I have a picture with the script in the inspector:

enter image description here

Script Inspector

And here is the console, trying to figure it out by using Debug.Log () for the calls of the function.

enter image description here

Using Debug.Log for the calls.

Upvotes: 0

Views: 136

Answers (1)

Dan Flanagan
Dan Flanagan

Reputation: 504

I tested it and it works fine under some conditions. The problem most likely is that your spawnNormPylonDis and spawnCoinPylonDis are public variables that are the same in the inspector, or spawnNormPylonDis is a multiple of spawnCoinPylonDis, pretty much with your code, you cannot have spawnNormPylonDis be 1. If they are then in the SpawnPylon function because it has already spawned one. You yourself commented.

//this makes shore that the GameObject is not spawned multiple times at aproximetley the same position.
currPosition = (int)transform.position.z;

I tested this with values of 2 and 3 and both functions got called, however, it would skip the second function if they were both divisible (6).

Therefore you got bottlenecked at this line of code because you set the currPosition after the first function call.

if (currPosition != (int)transform.position.z)

By making a basic wrapper function all is well in the world! So we set the currPosition after we checked both spawning calls, and voila, problem solved.

void SpawnPylon(int index, Transform[] spawnPoint, float dist, string debug)
{
    if ((int)transform.position.z % dist == 0)
    {
        //Debug.Log(debug);
        //this makes the GameObject spawn randomly
        spawnIndex = Random.Range(0, spawnPoints.Length);
        //This is instantiationg the GameObject
        GameObject go = Instantiate(pilons[index], spawnPoint[spawnIndex].position, spawnPoint[spawnIndex].rotation) as GameObject;
        go.name = string.Format("ID: {0}", index);
    }
}

private void SpawnMultiplePylons()
{
    if (currPosition != (int)transform.position.z)
    {
        SpawnPylon(1, spawnPoints, spawnNormPylonDis, "Spawn1");
        SpawnPylon(0, spawnPoints, spawnCoinPylonDis, "Spawn2");
        currPosition = (int)transform.position.z;
    }
}

I tested this and it worked. I hope that it also works for you!

Upvotes: 1

Related Questions