Search Unity

problems getting a variable to increase by 1

Discussion in 'Scripting' started by melonhead, Jan 24, 2020.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    i have a script from code monkey that takes a screenshot, i am trying to make it so it does not overwrite the previous screenshot by adding a number to the screenshot filename and then increasing the nuber by 1 each time but i just can not get it to work, i am trying to add a variable called i which gets increased before the screenshot is saved, any help grateful

    PROBLEM NOW FIXED SO I HAVE REMOVED CODE

    i hope it is ok to post the code here as it came from code monkey, i have asked there for help but got no reply, this thread can be deleted after help

    thank you
     
    Last edited: Jan 25, 2020
  2. ColinDarkwind

    ColinDarkwind

    Joined:
    Nov 26, 2013
    Posts:
    8
    Part of your problem is your for loop isn't actually encapsulating the logic below it. You need to wrap the logic you want to loop over with the curly braces.
    Code (CSharp):
    1. for(var i = 0; i <= 10; i++)
    2. {
    3.   // Do my logic here
    4. }
    5.  
    6. // Not: for(var i = 0; i <= 10; i++);
    You don't want that semicolon after your for loop.

    That said, I'm still not sure this is going to achieve what you are looking for. This is just going to write out 11 files all at once named CameraScreeenshot0.png to CameraScreenshot10.png of the same content.
     
  3. ColinDarkwind

    ColinDarkwind

    Joined:
    Nov 26, 2013
    Posts:
    8
    If what you are trying to do is make sure you save a file and don't overwrite any existing file, you could try this (untested)

    Code (CSharp):
    1. // Replace your for loop with this
    2. string path = string.Empty;
    3. int i = 0;
    4. do
    5. {
    6.   path = Application.dataPath + "/CameraScreenshot " + i + ".png";
    7.   i++
    8. } while(System.IO.File.Exists(path)); // Continue generating a file name until we find one that does not yet exist, incrementing i by 1.
    9.  
    10. System.IO.File.WriteAllBytes(Application.dataPath + "/CameraScreenshot " + i + ".png", byteArray);
    11. Debug.Log("Saved imaged to " + path);
     
  4. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    needed to put a ; after the i++, but still keeps overwritting the same file with the same name screenshot 1, the variable i is still not increasing to make the new filename

    everytime the function is calle it just keeps reading the i-0 which sets it back, this is the problem i ahave been having, i can not get the function to only read the i = 0 once, then increase it it just resets every time you press the space key t take the screenshot
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    If you declare a variable within { }, you need to learn about variable scope. I'll try to avoid to much deep discussion, but in this case, your i belongs to the if statement it's in, so nothing outside of it can access that i.

    Declare your i outside the if statement and then if you want, just set the value to 0 within the if statement.

    Also, when you do comparison. you need a ==, not just an =

    Code (CSharp):
    1. if(FIRSTCALL == true)
    or better yet

    Code (CSharp):
    1. if(FIRSTCALL)
     
  6. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    i have tried to declare it outside of the if statement but the function still will read it everytime itis called that is why i tried to get the variable read only once with the bool, but it still does not work, it will ot increase the value

    there is nowhere to to declare the variable i that only gets read once as it just gets reset back to 0
     
    Last edited: Jan 24, 2020
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You can declare the variable outside of any methods which would declare it once. Then, set it's value to 0 and increment it each time till you find a number is free. Then let it stay at that number. Next time you go to do a screenshot, it will start at that number. It will only reset back to 0 if you set it back to 0 after the first time. Sure, it's possible the user could delete a screenshot at #2 after they have taken 10 screenshots, but what does it matter? Do you really need to go back to create screenshot 2 again? Just keep going with #11.
     
  8. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    as i said, i have tried to declare it outside of the functions but it gives a red out of context error in the console,i have tried it in the take screenshot function also but still get errors, is there a way to just convert the date and time to a string and then add the string to the screenshot filename, this would seem easier but i just do not understand how to convert it to string, i have looked around but it is just confusing, if you could help me with this it would save me some headaches, thank you
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Code (CSharp):
    1. public class MyClass: Monobehaviour
    2. {
    3.    private int i;
    4.  
    5.    private void Start()
    6.    {
    7.        i = 0;
    8.    }
    9.  
    10.    public void TakeScreenshot()
    11.    {
    12.        do
    13.        {
    14.           path = Application.dataPath + "/CameraScreenshot " + i + ".png";
    15.           i++;
    16.        }while(System.IO.File.Exists(path));
    17.         System.IO.File.WriteAllBytes(Application.PersistantDataPath + "/CameraScreenshot " + i + ".png", byteArray);
    18. }
    19. }
    Typed in forum, so it may have typos, but the gist of it is there. You would not get a red error if i (a bad variable name for this, but I kept it for clarity) was declared outside a function properly.
     
  10. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    Decalring a variable before your start/update functions makes it global. You're declaring it out of scope. Don't be too fussy about global declaration for the sake of keeping it tidy at the moment if it's just breaking stuff.


    Code (CSharp):
    1. void Start()
    2. {
    3.    {int i;}
    4.    {i = 10;}
    5. }
    or

    Code (CSharp):
    1. void Start()
    2. {
    3.     int i:
    4. }
    5. void Update()
    6. {
    7.     int = 10;
    8. }

    won't work, it has to work through the routine like this:

    Code (CSharp):
    1. void FunctionName()
    2. {
    3.    int i;
    4.  
    5.    {i = 10;}
    6. }
    or

    Code (CSharp):
    1. public class MyClass: MonoBehaviour
    2. {
    3.    int i;
    4.  
    5.     void Start()
    6.     {
    7.          i = 10;
    8.     }
    9.  
    10. }
    You can declare variables inside an if routine if you're only going to use them in the routine (like the old int i; i++; i>10 for loops).
     
  11. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    630
    Thank you for help, now fixed so i have removed the original code
     
  12. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Why? This just makes the thread harder to follow for anybody having a similar problem.