Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to convert a string to a int like this in Unity (thank you Franz Gleichmann for answering twice now i've got new errors) here is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class Move : MonoBehaviour {
SerialPort sp = new SerialPort("/dev/tty.usbmodem1411", 9600); // set port of your arduino connected to computer
public bool on = false;
public int speed = 0;
public string A;
public string B;
void Start () {
sp.Open();
sp.ReadTimeout = 1;
}
void Update () {
A = sp.ReadLine();
if (sp.IsOpen) {
try {
if (on) {
bool isParsable = Int32.TryParse(A, out B);
if (isParsable)
{
Console.WriteLine(B);
}
else
{
Console.WriteLine("Could not be parsed.");
}
transform.Translate(Vector3.left + (Time.deltaTime * B / speed)); //error on this line
}
} catch (System.Exception) {
}
}
}
}
The aim of this script is to take a number from a USB and move an object by that amount. However, Unity gives me this error:
Assets/scripts/Move.cs(37,41): error CS0019: Operator '+' cannot be applied to operands of type 'Vector3' and 'float'
Any answers will help, if you want you can write your own version of the script. Thanks.
If you check the c# documentation of the Int32.TryParse function, you will notice the second parameter is of type Int32.
Your error:
CS1503: Argument 2: cannot convert from 'out string' to 'out int'
Is telling that your B variable should be Int32 to be able to store the result of the Int32 data.
public string A;
public Int32 B;
When you change that B variable to a numeric type, your second error Operator '*' cannot be applied to operands of type 'float' and 'string' raised at this line would also dissapear.
transform.Translate(Vector3.left + (Time.deltaTime * B / speed));
Some advice: You should start giving more meaningful names to your variables.
You must use int instead of Int32
So your code should look like this
bool isParsable = int.TryParse(A, out B);
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
using System;
class Program {
static string Main(){
string Input = Console.ReadLine();
return Input;
}
static int Health () {
int Health = 100;
return Health;
}
static int EnemyHealth(){
int EnemyHealth = 100;
return EnemyHealth;
}
static int y(){
int y = 0;
if (Main() == "d"){
y++;
if (y == 11){
y=10;
}
}
if (Main() == "a"){
y--;
if (y == -1){
y=0;
}
}
return y;
}
static int x(){
int x = 0;
if (Main() == "w"){
x--;
if (x == -1){
x=0;
}
}
if (Main() == "s"){
x++;
if (x == 11){
x=10;
}
}
return x;
}
}
I am trying to make a game that moves your character on repl with y and x cordinates but i get this error error CS5001: Program main.exe' does not contain a static Main' method suitable for an entry point. Btw this was made in replit. Please help!!!!!!
The Main() method has an expected signature (its name, arguments, and return type ect..). In your case you're using static string Main() which is not an allowable signature yet.
Per MSDN pertaining the Main() signature
Main can either have a void, int, or, starting with C# 7.1, Task, or Task return type.
If and only if Main returns a Task or Task, the declaration of Main may include the async modifier. Note that this specifically excludes an async void Main method.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've tried adding a ) where it says to, but that adds more problems. The error I get states:
Assets/LevelComplete.cs(12,64): error CS1026: ) expected.
What I'm trying to do here is use the if statement to find out if the Active Scene is in the specific range. Also, for a little more context, All I need is a range, and not the code to pick a number in that range if that helps.
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LEvelComplete : MonoBehavior
{
public void LoadNextLevel()
{
int Active12 = Range(1, 3);
int Load34 = UnityEngine.Random.Range(3, 5);
if (SceneManager.GetActiveScene().buildIndex = Active12
{
SceneManager.LoadScene(Load34);
}
}
}
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using Random = UnityEngine.Random;
public class question_63947858_script_error : MonoBehaviour
{
public void LoadNextLevel()
{
int Active12 = Random.Range(1, 3);
int Load34 = Random.Range(3, 5);
if (SceneManager.GetActiveScene().buildIndex == Active12)
{
SceneManager.LoadScene(Load34);
}
}
}
Things wrong:
MonoBehaviour was spelled wrong
System was clashing with UnityEngine namespace. So disambiguated with Random = UnityEngine.Random
No parentheses after the if statement
= changed to == in the if statement
"find out if the Active Scene is in the specific range."
...
var scene = SceneManager.GetActiveScene();
if (scene.buildIndex >= 1 && scene.buildIndex <= 3)
... or if you prefer Linq:
if (Active12.Contains(SceneManager.GetActiveScene().buildIndex))
See also How to elegantly check if a number is within a range?
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
private void button1_Click(object sender, EventArgs e)
{
Random random = new Random();
int getal1 = random.Next(0, 100);
Random random2 = new Random();
int getal2 = random2.Next(0, 100);
int Antwoord = getal1 + getal2;
if (Antwoord == textBox1.Text);
...
}
it says
Operator '==' cannot be applied to operands of type 'string' and 'int'
can someone help me?
if (Antwoord.ToString() == textBox1.Text);
Write it like this. You want to check int to string, this can't happen. You should convert the int value to string or the string value to int. I advice you to convert int to string in other case you can have an exception.
If you need an integer value, entered in the TextBox, you should try to parse its Text:
int textBox1Value;
if (int.TryParse(textBox1.Text, out textBox1Value))
{
// Here the text was successfully parsed to the textBox1Value variable
if (Antwoord == textBox1Value)
{
... // do your stuff
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am testing a simple method that has a ref parameter as a string and an out int parameter as a integer.
I pass in the string reference variable but I get an error every time that I pass in a variable to the out int age parameter.
Question: Why do i receive and Error when trying to pass in a parameter to an integer out field?
Error:
Not implemented exception was not handle is the error the pops up. Nothing is in the error window at the bottom of the screen like in some types of errors.
Changed code: (But still not working)
int t = 10;
bool a = p.testMethod("Test 0 ", out t);
Code:
namespace ConsoleApplication1
{
class Program
{
public bool testMethod(ref string text, out int age)
{
int z = 100;
age = z; <-- Assign the value here
return Int32.TryParse(text, out age); <-- return an string
}
static void Main(string[] args)
{
Program p = new Program();
bool a = p.testMethod("Test 0 Test",0); <-- Error on this line
bool b = p.testMethod("Test1Test",1); <-- Error on this line
bool c = p.testMethod("Test22 TEST",2); <-- Error on this line
}
}
}
Because you are passing as out parameter a constant and the same for the ref variable and do not put the appropriate keywords in front of the parameters passed to the testMethod
Program p = new Program();
int possibleValue;
string test = "Test 0 Test";
bool a = p.testMethod(ref test, out possibleValue);
However, as is, there is no need to pass the string as ref. You are not trying to change it inside the testMethod, so you could safely remove the ref for that parameter
An out parameter needs a variable as input so that it can affect the value.
You need to change the call to something like:
int outVal;
bool a = p.testMethod("0", out outVal);
This allows the calling function to set the value of 'outVal' so that the calling function can use the new value.
I also changed the "Test 0 Test", because the TryParse function will not be able to properly parse a value from that string, since it contains text. You will also need to change the other 2 calls.
Basically, the testMethod will parse the string, and put the parsed integer value into the age variable. So the line that sets age=z (or 100) really does not matter, since the value of age then gets changed to the result of the string parse.
And, since you are not changing the value of 'text', you do not need to pass as a reference so change the line:
public bool testMethod(ref string text, out int age)
to
public bool testMethod(string text, out int age)
You have to use the out reserved word in the calling parameter list to be able to use the out in the method.
public void testMethod(ref string a, out int b)
{
//code here
}
Your calling method has to look like the below call.
string g = "I Like Captain America";
int t = 10;
p.testMethod(ref g, out t);
When I first started using C# this seemed like and odd way of programming. Why even bother with this type of syntax. But as I did more programming I found that the out parameter was extremely useful when I had to return more than one value from a function (although you should only return one parameter from a function at any time in good programming practice) but there seems to every know and then be the case where things happen and you have fudge the rules a bit. In your example you would not really need to use the ref for the string.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to calculate the total for a list in my C# program, I have gotten help from several people in my class and we can't seem to find the problem, my code is,
int totalB = 0;
Cards.ForEach(delegate(ConsoleApplication1.Program.CreditCard Balance)
{
totalB= totalB + Balance;
});
The error is this Error 1 Operator '+' cannot be applied to operands of type 'int' and 'ConsoleApplication1.Program.CreditCard'
Any help for this would be much appreciated as I have no idea and neither do the people that tried to help me with this issue
I'm guessing you have a class like:
partial class CreditCard
{
public int Balance {get; set;}
}
So following what you have, explicitly, you most likely intended:
int totalB = 0;
Cards.ForEach(delegate(ConsoleApplication1.Program.CreditCard card)
{
totalB = totalB + card.Balance;
});
This iterates over each item in your Cards collection, and adds the value of the Balance property to totalB. Note I have called the variable card in the delegate to further illustrate what is going on - the delegate will be called once for each item in the collection. Inside, you can pick out the Balance property and add it to totalB.
Note you can also do this in a number of other ways:
Using LINQ:
int totalB = Cards.Sum(card => card.Balance);
Using a lambda expression instead of an explicit delegate:
int totalB = 0;
Cards.Foreach(card => {totalB += card.Balance;});
Using a foreach loop:
int totalB = 0;
foreach(CreditCard card in Cards)
totalB += card.Balance;
(If you are not familiar with it, the x += y is the same as x = x + y.)
As far as getting sum of list is concerned. it is as simple as (assuming Cards is a list)
Cards.Sum(x=>x.YourPropertyToSum);
Your Error:
The error is this Error 1 Operator '+' cannot be applied to operands of type 'int' and 'ConsoleApplication1.Program.CreditCard'
you are trying to add an int with ConsoleApplication1.Program.CreditCard (what is this) which is obviously not a type that can be added to int. Hence the error you are getting.