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
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.
for(var i = 0; i <= 10; i++)
{
// Do my logic here
}
// 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.
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)
// Replace your for loop with this
string path = string.Empty;
int i = 0;
do
{
path = Application.dataPath + "/CameraScreenshot " + i + ".png";
i++
} while(System.IO.File.Exists(path)); // Continue generating a file name until we find one that does not yet exist, incrementing i by 1.
System.IO.File.WriteAllBytes(Application.dataPath + "/CameraScreenshot " + i + ".png", byteArray);
Debug.Log("Saved imaged to " + path);
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
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 =
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
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.
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
public class MyClass: Monobehaviour
{
private int i;
private void Start()
{
i = 0;
}
public void TakeScreenshot()
{
do
{
path = Application.dataPath + "/CameraScreenshot " + i + ".png";
i++;
}while(System.IO.File.Exists(path));
System.IO.File.WriteAllBytes(Application.PersistantDataPath + "/CameraScreenshot " + i + ".png", byteArray);
}
}
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.
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.
void Start()
{
{int i;}
{i = 10;}
}
or
void Start()
{
int i:
}
void Update()
{
int = 10;
}
won’t work, it has to work through the routine like this:
void FunctionName()
{
int i;
{i = 10;}
}
or
public class MyClass: MonoBehaviour
{
int i;
void Start()
{
i = 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).