Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by awplays49 · Apr 10, 2015 at 09:43 PM · ifelse

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image winsjansen · Apr 10, 2015 at 10:59 PM 0
Share

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?

avatar image awplays49 · Apr 11, 2015 at 02:52 PM 0
Share

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.

avatar image Bonfire-Boy · Apr 11, 2015 at 03:25 PM 0
Share

Are you saying that it seems to be entering both the "if" clause and the "else" clause?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Apr 11, 2015 at 07:27 PM 0
Share

With regard to the PlayerPrefs parts of that, my assumption was that this isn't the only script in the project.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges