Problems with a variable defined in a for loop C# - c#

Me and my friend have been hacking at it for hours but we just can't figure out what's wrong with it. It's essentially running through an array and if the button should be locked or interactable, and if it's null it'll be interactable. By using player prefs these settings should persist through each session with the app.
Here's the code:
for (i = 0; i < buttons.Length; i = i + 1) {
if (PlayerPrefs.GetInt("button" + string.Format i) == null) {
PlayerPrefs.SetInt("button" + string.Format i, 1);
}
if (PlayerPrefs.GetInt("button" + string.Format i) == 1) {
button.interactable = true;
} else {
button.interactable = false;
}
}
Currently unity is displaying 5 errors:
error CS1525: Unexpected symbol `i' (2 of these)
error CS1519: Unexpected symbol `else' in class, struct, or interface member declaration
error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
error CS8025: Parsing error

Just a guess, but you should write:
for (int i = 0; i < buttons.Length; ++i) {
You maybe forgot to declare i
Also this line:
PlayerPrefs.GetInt("button" + string.Format i)
string.Format is a static method of string. The syntax is wrong. You can write it this way:
PlayerPrefs.GetInt("button" + i)
Or this way:
PlayerPrefs.GetInt(string.Format("button{0}",i));

Related

How to check if two strings length are equal in c#?

I'm new in c# and unity and Wondering how could I write this code to check if two string lengths are equal.
with this code unity system shows this error:
error CS1061: Type char' does not contain a definition forLength' and no extension method Length' of typechar' could be found.
for (int i = 0; i < Answers.Length; i++)
{
if (GetAnswer[i].Length == Answers[i].Length)
{
//Do something
}
}
if (yourString.Length == yourOtherString.Length)
{
//dosomething
}
should check if a string is equals in length to another
The problem with your code is that a string is an array of char so it calculate the length of the char (Which do not exist)
for (int i = 0; i < Answers.Length+1; i++)
{
if (GetAnswer.Length == Answers.Length)
{
//Do something
}
}
You also need to increment the value of the for to get the correct length otherwise the Answers.Length will always be short of 1

How to get and compare current scene name

I tried to compare scene name but I am getting the error below:
error CS0019: Operator ==' cannot be applied to operands of type
method group' and `string'
I can't figure out how to fix this error. please help me! the error is located on the arrow
void CheckCurrentLevel()
{
for(int i = 1; i < LevelAmount; i++)
{
---> if (SceneManager.LoadScene == "Level" + i) {
CurrentLevel = i;
SaveMyGame ();
}
}
}
SceneManager.LoadScene is a void function used to load a scene. It does not return anything so you can't compare it with a string.
It looks like you want to compare the current scene name with "Level" + i. If that's true then you are looking for SceneManager.GetActiveScene().name
void CheckCurrentLevel()
{
for (int i = 1; i < LevelAmount; i++)
{
if (SceneManager.GetActiveScene().name == "Level" + i)
{
CurrentLevel = i;
SaveMyGame();
}
}
}
Keep in mind that the == operator Checks if the values of two operands are equal or not, subject to the condition that both the operands should be same type. From the error message it is clear that LoadScene is defined as a method.
Your code will works fine if you use like this:
if (SceneManager.LoadScene(params if any) == "Level" + i)
{
// Code here
}
Subject to the condition that LoadScene() method should return a string object or else the return type will be a business object in this case use a string property for comparison

How do I best handle arguments with modifiers in a console program?

I'm writing an argument parser for a console program, but I'm certain that my current implementation is not the best way to do this. Since this is a common problem, is there a standard/better/best way to pass multiple arguments with optional modifiers to a console program?
private static void Main(params string[] args)
{
for (int i = 0; i < args.Length; i++)
{
// Is there a valid argument?
if (args[i] == "/r")
{
Console.WriteLine("Argument: /r");
// Is there a successive argument?
if (i + 1 < args.Length)
{
// Is the successive argument an argument or a modifier?
if (args[i + 1].IndexOf("/", StringComparison.CurrentCultureIgnoreCase) == -1)
{
Console.WriteLine("Argument: /r " + args[i + 1]);
}
}
}
}
}
The main problem I see with this implementation is that I'd have to rewrite the last two if blocks for each argument and modifier, which would become unwieldy for a large program, I think.

Why am I getting this Error in C# Compiler "Unassigned Local Variable" [duplicate]

This question already has answers here:
c# switch problem
(8 answers)
Closed 9 years ago.
I am getting the following error. Use of unassigned local variable markduplicate. I don't understand why? This program finds a duplicate in an array. I been trying to figure it out and I feel like im so close. Thanks for the help.
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
Console.Write("Element " + i + ": ");
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
bool markduplicate;
for (i = 0; i < Array_Size; i++)
{
for (int j = 0; j < Array_Size; j++)
{
if (i != j)
{
if (number[j] == number[i])
{
markduplicate = true;
}
}
if (markduplicate != true)
{
Console.WriteLine("Element " + i + " " + number[i]);
}
}
}
}
}
This is because the C#'s code analyzer detected that there are paths through you program when the markduplicate's value would be referenced before any assignments to it are made. Specifically, this is going to happen during the very first iteration of the nested loop, when both i and j are zero: the if (i != j) block containing the assignment is not going to execute, so the value of the markduplicate is going to get retrieved in the very next if statement.
To fix this problem, change
bool markduplicate;
to
bool markduplicate = false;
in the declaration.
The compiler thinks it is possible for you to hit the if (markduplicate != true) line before markduplicate has been set.
If you think the compiler is wrong, give it a value when declaring e.g. bool markduplicate = true;. If you analyze and think the compiler is correct, adjust you code accordingly.
Also: if (markduplicate != true) is considered poor style. Use if(!markduplicate) instead.
You've declared the boolean variable markduplicate but you haven't given it an initial value - the compiler doesn't know if it should be initially true or false.
In this case it's clear you want it to be initially false, so put this:
bool markduplicate = false;
it will now compiled.
In your code, (i != j) is false before it is true and therefore you are checking the value of your variable which has had nothing assigned to it.
You need to either assign a value to markduplicate at its declaration or you need to make sure that any path that leads to a conditional check on its value has a value assigned to it first.
You have to assign markdefault to either true or false in your declaration. Otherwise, if number [j] != number [i], then markdefault will not be assigned and if markduplicate != true cannot be evaluated.

Getting syntax error involving ';'

I am making a pokemon game and this section is giving me 3 errors:
"Invalid expression term ';' (CS1525)" and "; expected(CS1002)"
public class HeldItem
{
public static int CritCalc(bool item,bool skill, bool UsedItem,int dmg)
{
Random rand=new Random();
Action jump=new Action();
int i = rand()%100;
double CritPerc = 6.25;
if(item==true)
CritPerc=12.5;
else if(skill==true)
CritPerc=12.5;
else if(UsedItem==true)
CritPerc=12.5;
else if((item==true & skill== true) || (item==true & UsedItem == true) || (skill==true & UsedItem==true))
CritPerc=25%;
else if(item==true & skill == true & UsedItem==true)
CritPerc=33.3%;
if(Action) //jump
CritPerc = 50%;
if(i<CritPerc)
dmg=2*dmg;
else if(i>CritPerc)
dmg==dmg;
return dmg;
}
}
}
Maybe it is a silly problem but I don't know what it is
You cannot specify percents in C#.
You have the following lines:
CritPerc=25%;
CritPerc=33.3%;
CritPerc = 50%;
That is invalid (Percent indicates the modulo operator in C#).
Instead, you probably want to specify the values as double floating point values.
CritPerc=0.25;
CritPerc=0.333;
CritPerc = 0.50;
%(percent) operator in c# means modulo operation which takes two
operand. but you give one. So it gives error.
Instead of
CritPerc=25%;
write
CritPerc=.25;
or
CritPerc=25/100;
and
dmg==dmg
causes error.
The line that says:
dmg == dmg;
Ah, the fatal '=' error.
You have dmg ==dmg which is the wrong operator and if dmg already has the correct value just return it, dmg=dmg goes without saying

Categories