How do I solve this problem with my code? C# [closed] - c#

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?

Related

Problems with converting string to int [closed]

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);

multiplication in C# basic program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
In the Main function, declare three integer variables (name them arbitrarily) and initialize these variables with values (ideally different). Write a program that computes the following arithmetic expression: Multiply the values of the last two variables and subtract the value of the first variable from the obtained result. Write the arithmetic expression and its result on the screen in a suitable way.
using System;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int prvni = 10;
int druha = 20;
int treti = 30;
int vysledek = (treti * druha) - prvni;
Console.WriteLine("Výsledek: {vysledek}");
}
}
}
String-interpolation in that way requires a $ (dollar-sign) before the string to specify that you are doing interpolation, so: Console.WriteLine($"Výsledek: {vysledek}");
For more examples on string interpolation: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
An alternative solution could be to simply concatenate the variable to the string: Console.WriteLine("Výsledek: " + vysledek);
You need to print the varable correctly.
Console.WriteLine("the answer {0}", vysledek);
Take care,
Ori

Autodesk guide project two "a namespace cannot directly contain members from" [duplicate]

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 9 years ago.
Improve this question
I am trying to use this code for NET.reflector using Reflexil. I am trying to replace code with this:
if(Input.GetKeyDown(KeyCode.Keypad5)) {
int i = 0;
Character localPlayer = PlayerClient.GetLocalPlayer().controllable.GetComponent<Character>();
foreach (UnityEngine.Object obj2 in UnityEngine.Object.FindObjectsOfType(typeof(LootableObject)))
{
if (obj2 != null)
{
i++;
LootableObject loot = (LootableObject) obj2;
Debug.Log("Loot "+i+": "+loot.transform.position.ToString());
CCMotor ccmotor = localPlayer.ccmotor;
if(ccmotor != null && tpPos1 != Vector3.zero) {
ccmotor.Teleport(loot.transform.position);
Notice.Popup("", "Teleported to "+loot.name, 1.5f);
}
break;
}
}
}
But it gives me an error when I try to compile:
Line: 1 Column: 1 Error Number: CS0116 Error Message: "A namespace does not directly contain members such as fields or methods"
This is Unity code I think. I am not that experienced. Could anyone fix this for me? Or tell me what to do? Thanks!
The snippet you're showing doesn't seem to be directly responsible for the error.
This is how you can CAUSE the error:
namespace MyNameSpace
{
int i; <-- THIS NEEDS TO BE INSIDE THE CLASS
class MyClass
{
...
}
}
If you don't immediately see what is "outside" the class, this may be due to misplaced or extra closing bracket(s) }.

CS1513 C# } expected Error [closed]

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 5 years ago.
Improve this question
I am really knew to programming so please be nice haha. Excuse the "Noob" Question, I'm just experimenting right now. Can anyone give me tips on what is causing
CS1513 C# } expected
My code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
string country = "USA";
Console.WriteLine("Hello, What country are you from?");
string countryName = Convert.ToString(Console.ReadLine());
if (countryName == country) ;
{
Console.WriteLine("You are Eligable for the competition ! :-) ");
}
else {
Console.WriteLine("You are not Eligable, Sorry!!");
}
}
}
}
I'm trying to give an answer based on the country of the user basically.
Remove the ; from if (countryName == country) ;
The ; is a statement terminator. See why do some lines not have semicolon in C#?
You put a ; after your if statement.
It should be if (countryName == country) instead of if (countryName == country) ;

Why can't i convert from Fahrenheit to Celsius? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
My C# code keeps returnng me a wrong conversion result. As far as i know, the conversion formula is correct, but it keeps displaying wrong results.
e.g: 70°F gives me 12,777777 °C. Can you check my code out ?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Temperature
{
class Program
{
class Temperature
{
public double temp;
public void Convert(double value)
{
double tEmp;
tEmp = (value - 32) / 1.8;
Console.WriteLine("The temperature in °C is :" + tEmp);
Console.ReadKey();
}
}
static void Main(string[] args)
{
double f;
Temperature c = new Temperature();
f = Console.Read();
c.Convert(f);
}
}
}
your problem is
f = Console.Read();
It just reads the first character, not your entire line of input. Try
f = Convert.ToDouble(Console.ReadLine());
Here's a good answer on the difference between Console.Read vs. Console.ReadLine()
Console.Read() returns the ordinal value of the next character in the input stream.
The first character in your input stream is '7' which has an ordinal value of 0x0037. Represented as decimal this is 55 and (55-32)/1.8 is 12.7777.
You should be using Console.ReadLine() rather than Console.Read().

Categories