- Home /
Why doesen't the function get called the second itme?
So 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 `SpawnPylon()` so I can spawn the pylons with it but for some reason the function get's 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:
And here is the console, trying to figure it out by using Debug.Log () for the calls of the function.
Answer by bobisgod234 · Apr 19, 2017 at 02:48 AM
Once you successfully spawn one pylon, you set your currPosition variable to be the transforms Z position, so your if condition
if (currPosition != (int)transform.position.z)
Will prevent you from spawning another until the gameObject with your "CameraScript" component moves along the Z axis by some amount.
@Sarmale32 in a more general sense, theres 2 if conditions just before the Debug.Log line that doesn't get executed. Even if you can't find the problem by reading your code, placing more Debug.Logs outside the if-clauses to see the values of your conditions would help you find the issue. Basic debugging :)
Your answer
Follow this Question
Related Questions
Call a function once from OnGUI 2 Answers
touch buttons function OnGUI issue 1 Answer
How to call functions? 1 Answer
Function isn't being called 1 Answer