For loop doesn't work after compiling
I have a script that should spawn my falling platforms back into their places if the player dies. This works fine in the editor (and in Unity Remote 5) but after compiling it doesn't work anymore. First I used foreach loops but it didn't work. When I read that there are problems with them I changed my script to use normal for loops but the problem still exists after build. Here are the parts of the code that don't execute after build:
void Start () {
platforms = GameObject.FindGameObjectsWithTag ("fallingGround");
fallingPositions = new Vector2[platforms.Length];
for (int i = 0; i < platforms.Length; i++) {
fallingPositions [i] = platforms[i].transform.position;
Debug.Log(fallingPositions[i]);
}
}
public void playerDied () {
player.transform.position = spawnPoint.transform.position;
lifeCount--;
for (int i = 0; i < platforms.Length; i++) {
GameObject platform = platforms[i];
platform.transform.position = fallingPositions [i];
platform.GetComponent<Rigidbody2D> ().mass = 10000;
platform.GetComponent<Rigidbody2D> ().gravityScale = 0;
platform.GetComponent<FallingPlatformScript> ().playerEntered = false;
}
}
Forgot to mention that I'm making this game for android.
Can you elaborate on how it doesn't work? What's not happening that should be happening, and what's happening that shouldn't be happening?
I don't see anything obviously wrong in this code, you'll need to show us more code than this.
Sure,
what the code above does is that in Start() it makes an array of the positions of all the falling platforms.
Then when the player dies the PlayerDies() is called and it should return the falling platforms to their starting positions. This doesn't happen in the build version.
As I mentioned that in the editor the script works exactly as intended but after I make a build and run the game in my phone everything else works but the PlayerDies() script.
Answer by sokkasokka · Mar 18, 2017 at 04:14 PM
Solved this by moving following code inside the script in the falling platforms.
for (int i = 0; i < platforms.Length; i++) { GameObject platform = platforms[i]; platform.transform.position = fallingPositions [i]; platform.GetComponent ().mass = 10000; platform.GetComponent ().gravityScale = 0; platform.GetComponent ().playerEntered = false; }
Still wondering why this worked in the editor but not in the build. Code is more elegant now tough.