Hello,
I am new to the C# I am facing same issue with the same error. can anyone point out whats wrong with my code.
It would be great help. thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log(“Bird Game”);
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(“right”))
Flip(“right”);
{transform.Translate (.04f,0f,0f);
}
if (Input.GetKey(“left”))
Flip(“left”);
{transform.Translate (-.04f,0f,0f);
}
if (Input.GetKey(“up”))
{transform.Translate (0f,.04f,0f);
}
if (Input.GetKey(“down”))
{transform.Translate (0f,-.04f,0f);
}}
}
public void Flip( string direction){
var thescale = transform.localScale;
Use code tags when posting code (which will give us formatting, tabs, and line numbers) and copy and paste the entire error message (which will, among other things, point to the exact line of code where the problem is).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float distance;
public int count;
public string message;
private bool finished;
// Start is called before the first frame update
void Start()
{
Debug.Log("Bird Game");
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("right"))
//Flip("right");
{transform.Translate (.04f,0f,0f);
}
if (Input.GetKey("left"))
//Flip("left");
{transform.Translate (-.04f,0f,0f);
}
if (Input.GetKey("up"))
{transform.Translate (0f,.04f,0f);
}
if (Input.GetKey("down"))
{transform.Translate (0f,-.04f,0f);
}}
}
public void Flip( string direction){
var thescale = transform.localScale;
if(direction == "right") {
thescale.x = 1;
} else {
thescale.x = -1;
}
transform.localScale =thescale;
}
The error points to line 53 and you’ve posted a 50-line script… something’s missing. Where does it go when you double-click the error?
I can say for sure that your { curly brackets } are a mess. My first suggestion would be that you should try putting every curly bracket on its own line, and see if they match up correctly. Generally, the error means that you have something outside of the class block. So like:
public class NewBehaviourScript : MonoBehaviour
{
blah blah blah
} //this matches with the { right after the "class" line
public void Flip(string direction) { // This is bad, it needs to be INSIDE the above { }'s
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public float distance;
public int count;
public string message;
private bool finished;
// Start is called before the first frame update
void Start()
{
Debug.Log("Bird Game");
}
// Update is called once per frame
void Update()
{if (Input.GetKey("right"))
//Flip("right");
{transform.Translate (.04f,0f,0f);}
if (Input.GetKey("left"))
//Flip("left");
{transform.Translate (-.04f,0f,0f);}
if (Input.GetKey("up"))
{transform.Translate (0f,.04f,0f);}
if (Input.GetKey("down"))
{transform.Translate (0f,-.04f,0f);}
} }
public void Flip( string direction){
var thescale = transform.localScale;
if(direction == "right") {
thescale.x = 1;
} else {
thescale.x = -1;
}
transform.localScale =thescale;
}
Sorry for being so messy. Some how I have edited the code after taking the screen shot , actually this is my first day of learning script.
attaching new screen shot.
BTW, you can just ctrl-C and directly copy the error out of the console, no need for screenshots.
My advice is still the same, put every { and } on a line by itself, that way you’ll be able to match them up and see what is outside the main class’s brackets, and move it inside.
As StarManta pointed out, your formatting is a mess. That’s going to create bugs. If you don’t follow a particular format and be consistent about it, you make your own code harder to read. If it’s harder to read, then it’s harder to spot what’s wrong with it.
Fix your formatting first, then review your code to see what’s wrong. What IDE are you using? If you’re using Visual Studio Community or Visual Studio Code, it should’ve helped you auto format your code.
In this particular case, here are where I can see some stray brackets. The first bracket is actually fine but very confusing to read because it looks like it’s part of the if statement, and not as an opening bracket for Update():