This is my code so far, but i can’t find the missing {
I use Visual Studio Code and Unity 2020.
Thanks if you can help me.
using UnityEngine;
public class WeaponSwitching : MonoBehaviour {
public int selectedWeapon = 0;
// Start is called before the first frame update
void Start() {
SelectWeapon();
}
// Update is called once per frame
void Update() {
if (Input.GetAxis(“Mouse ScrollWheel”) > 0f)
{
int i = 0;
foreach(Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}
No one memorizes error codes, which makes the error code the most useless part of the error message. In the error message you are seeing it has a text description which tells you what the problem is. It also has a line number for which line the error occurs. That’s the information which is important.
could you still help me to search the missing {, because I cant find it
EDIT: Thanks for the clue with the code line, but when I add one at this point the error message disappars and another one appears:Type or namespace definition, or end-of-file expected
But the error message says the line number in parenthesis. There shouldn’t be any searching, you go to the line it says. It is either a problem on that exact line, or one really close. You posted your code without any formatting, so finding a missing parenthesis is really difficult for anyone here. Take a look at how other people post code, or the sticky thread about CODE tags at the top of the scripting forum.
Here’s basically the info you see in an error message:
Error Code : Error Message (line, column)
Go to whatever line number in your code the error message says.
This is your problem line: if (selectedWeapon = transform.childCount -1);
It should be: if (selectedWeapon == transform.childCount -1)
This was easy to find in Visual Studio because it is underlined in red. You need to be exact when doing your code otherwise you are going to just keep running into these issues. Make sure you are also using something like Visual Studio that will highlight your errors as it should make it easier to find them.
And for the next time you need to post a script, just to reiterate what has already been said:
Post the full error message next time as that will include the line/character position.
Use code tags when posting your scripts so these can be read easily by everyone else here.