- Home /
Both if and else are running, because else condition is met after if
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CubeManager : MonoBehaviour {
public GameObject CubeGet;
public List <GameObject> Cubes = new List <GameObject> ();
public int CurrentCube;
public int MinCubes;
public int MaxCubes;
public int MinX;
public int MaxX;
public int MinY;
public int MaxY;
public int [] AllSortOrders;
public int HighestLayer;
public Sprite [] Sprites;
void Start () {
int ActionGet = PlayerPrefs.GetInt ("Cube Manager Action");
if (ActionGet == 0)
{
int CubesToSpawn = Random.Range (MinCubes, MaxCubes);
for (int CubesSpawned = 0; CubesSpawned < CubesToSpawn + 1; CubesSpawned ++)
{
Instantiate (CubeGet, new Vector3 (Random.Range (MinX, MaxX + 1), Random.Range (MinY, MaxY + 1), 1), Quaternion.identity);
if (CubesSpawned == CubesToSpawn)
{
CurrentCube = 1;
Camera.main.transform.position = new Vector3 (Cubes [1].transform.position.x, Cubes [1].transform.position.y, -1);
}
}
PlayerPrefs.SetInt ("Cube Manager Action", 1);
}
else
{
int CubeCount = PlayerPrefs.GetInt ("Cube Count");
for (int CubeRespawn = 1; CubeRespawn < CubeCount + 1; CubeRespawn ++)
{
Instantiate (CubeGet, Vector3.zero, Quaternion.identity);
}
CurrentCube = PlayerPrefs.GetInt ("Current Cube");
}
}
void OnApplicationQuit () {
PlayerPrefs.SetInt ("Cube Count", Cubes.Count);
}
}
Where can I put
PlayerPrefs.SetInt ("Cube Manager Action", 1);
in a place that doesn't trigger the else as well?
That's not how "if else" works, this "ELSE" statement can never ever run if the "IF"statement already did, that sort of how the idea of it works..
IF this is green, then run this block of code, ELSE run this other block of code, it doesn't read the IF first then read the ELSE after it has run the IF block, it reads both and THEN deter$$anonymous$$e which one it will run or not.
Your problem must be somewhere else, do you have this script attached to multiple objects? Do you got other scripts?
well its weird because when i took out the code that was happening in the else, it stopped. however, the debug said it wasn't running anyways.
Are you saying that it seems to be entering both the "if" clause and the "else" clause?
Answer by equus_ligneus · Apr 11, 2015 at 06:39 PM
You use PlayerPrefs to store whether your cubes have been spawned in this run (Play-Mode, game start). PlayerPrefs persist between runs (because they are data on disk, not in the RAM). Means: after one run, you will NEVER EVER get into the if-part again, but straight to the else part. To fix this, you'd have to reset your PlayerPrefs after each run (either deleting the created PlayerPrefs or setting all your PlayerPrefs to your standard value in the OnDestroy()-method) or, instead of using PlayerPrefs, store your progress in a member variable of CubeManager (preferrably, a bool). If you want to spawn your cubes in only one run and have them persist through runs (in which PlayerPrefs to check for Instantiation like you do are valid) I suggest saving their data to disk as well (in some data-container-format like xml, PlayerPrefs are not made to store large amount of data).
But there are a few other things that bother me as well:
You instantiate cubes but don't add references to said cubes to your cube list. Then you access cubes in your list... If the cubes do not add themselves to the list, this will throw Exceptions. If they do not add themselves, then swap line 27 with this:
GameObject go = Instantiate(CubeGet, new Vector3 (Random.Range (MinX, MaxX + 1), Random.Range (MinY, MaxY + 1), 1), Quaternion.identity) as GameObject;
Cubes.Add(go);
You have to do similar steps in line 41.
Your for-loop in line 25 loops CubesToSpawn + 1 times... remove the + 1 if you want it to loop CubesToSpawn times. Same for line 39.
The if-clause in line 28 (where you check whether the current element is the last) is obsolete. Just put everything that is in there below for-loop (where you have PlayerPrefs.SetInt ("Cube Manager Action", 1);) and it will automatically be called after you have instantiated all your cubes (like before)
You start accessing the list at index 1. In most (if not all) programming languages that feature Arrays or Lists, the first element is at index 0. You basically skip the first element and access the second when you set the camera in line 31. Most likely this is behaviour you don't want and if you only spawn 1 cube, this will throw Exceptions.
Since the else-clause does not seem to be final (assuming you want to load your cubes from disk which is way out of the scope of this answer) I can only say that you seem to be accessing a PlayerPref-value ("Current Cube") you did not set previously.
P.S.: If my answer sounded rude I apologize. I tend to be painfully direct.
With regard to the PlayerPrefs parts of that, my assumption was that this isn't the only script in the project.
Your answer
Follow this Question
Related Questions
İf-Else block doesn't react the condition 2 Answers
if statement for 6 ints help C# 2 Answers
problem with instance 1 Answer
Else and If 2 Answers
How stop a piece of code from running if the conditions are no more met. 1 Answer