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
Related
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
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));
i have a project in which i have assigned some functions to single characters(e.g. Keyboard Key "H" will do high pass filtering).
Now To get "H" as an output i have created a down event which hopefully pick up the keys are down and calls the function with an integer value. However I get an error when i try to compare the input value with an integer value in the function. The following is my code...
public static event DownEventHandler Down;
public static delegate void DownEventHandler(string Key);
Down(FunctionChar((Keys)lParam.Code)); // lParam.code is an integer value.
private string FunctionChar(Keys e)
{
if(e >=65 && e<=90){
if (Control.IsKeyLocked(Keys.CapsLock) || ((Control.ModifierKeys !=0) && (Keys.Shift) != 0))
{
return e.ToString;
}
else
{
return e.ToString.ToLower;
}
}
I assume that this function will give me the output a string either "G" or "g". as mentioned before i want to use it in further functionality.
However it gives me error as following.
Operator '>=' cannot be applied to operands of type 'System.Windows.Forms.Keys' and 'int'
I know one of the solution is to use SWITCH statement but i want to use if statement and not switch.
Can some one tell me - what is the problem? What values does "e" posses and how can i convert it to (Int) so i can use it in the IF statement.
You can't compare System.Windows.Forms.Key with and Integer so you have to convert the key has been converted to an integer before you compare them. here is an example for you:
Keys e = Keys.A;
int keyVal= (int)e;// return 65
So you can do like this:
if((int)e >=65 && (int)e<=90)
{
// your code goes here
}
Another way for doing this is:
if(e >= Keys.A&& e<= Keys.Z)
{
// your code goes here
}
Update :
You can return the corresponding character using : return ((char)e).ToString();
Hence the whole function signature will be like the following:
private string FunctionChar(Keys e)
{
if ((int)e >= 65 && (int)e <= 90)
{
if (Control.IsKeyLocked(Keys.CapsLock) || ((Control.ModifierKeys != 0) && (Keys.Shift) != 0))
{
return ((char)e).ToString();
}
else
{
return ((char)e).ToString().ToLower();
}
}
return "";
}
When working with an enum, such as Keys, it's better design to express any conditions in terms of the enum's values, rather than casting back to ints.
So I'd recommend replacing:
if(e >=65 && e<=90)
with
if (e >= Keys.A && e <= Keys.Z).
This should compile fine - and your intention is clearer to anyone reading your code!
I'd rather not use magic numbers but actual chacraters, and I'd write an extension method(s) for this:
public static class KeysExtensions
{
public static Boolean IsLetter(this Keys value)
{
return value >= Keys.A && value <= Keys.Z;
}
}
So when handling events you can put readable code:
private void Something_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.IsLetter())
{
// Do your operations here...
}
}
I am looping through a list and would like to add multiple occurrences, should I find them.
so far I have,
public struct optionsSort
{
public string name;
public string date;
public double strike;
public string callPut;
public double size;
public string isin;
}
List<List<optionsSort>> stocks = new List<List<optionsSort>>();
optionsSort tempStock1 = new optionsSort();
List<optionsSort> posCheckOptions = new List<optionsSort>();
then some code, then,
for(int k = 0; k<posCheckOptions.Count; k++)
{
for(int l = 0; l<posCheckOptions[l].Count; l++)
{
if(posCheckOptions[l+1] == null)
{
//finished the iteration
break;
}
else if
(posCheckOptions[k][l + 1].date == posCheckOptions[k][l].date
&& posCheckOptions[k][l + 1].strike == posCheckOptions[k][l].strike
&& posCheckOptions[k][l + 1].callPut == posCheckOptions[k][l].callPut)
{
posCheckOptions[k][l].size = posCheckOptions[k][l].size
+ posCheckOptions[k][l + 1].size;
}
}
}
Basicly, Im looking forward from the start of the list. asking the question, are certain elements of the list at i+1 the same as i, if so, add those elements to i and delete the entire row.
i get this error
"Error 1 Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable C:\Users\WindowsFormsApplication1\WindowsFormsApplication1\ReadCSV.cs 890 25 WindowsFormsApplication1
"
Many Thanks for looking.
I believe your problem is that you are using a mutable struct. Mutable structs are evil.
The simplest solution is to change optionsSort to a class. That should fix the error message.
To explain the error message, when you call posCheckOptions[k][l], since optionsSort is a struct, it returns a copy of the value in the list. When you change size, it will update the copy, but not the one in the list. The copy would then be discarded. The compiler recognizes this and stops you.
I recommend you read up on the differences between reference types and value types.
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.