Having trouble with ',' Syntax error message.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChameleoonCube : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown(KeyCode.1))
{
GetComponent().material.color = Color.black;
}
if (Input.GetKeyDown(KeyCode.2))
{
GetComponent().material.color = Color.yellow;
}
if (Input.GetKeyDown(KeyCode.3))
{
GetComponent().material.color = Color.magenta;
}
}

So that is my code, trying to change the color of an object upon key clicks and i keep getting a syntax error that a comma is needed where the period is in “(KeyCode.1)” What am I missing ?

What does the documentation for KeyCode suggest about identifiers for that key?

1 Like

Here’s the docs, always consult them before reaching for the forums: Unity - Scripting API: KeyCode

Good luck.

Just as a general syntax rule: in C# ANY idenfier MUST NOT start with a number (let alone just be a number). So this is syntactically impossible. That’s actually true for the majority of programming languages (C, C++, Javascript, lua, Java, Python, …). The only language I can currently think of that does somewhat allows identifiers to start with numbers is SQL, but even then it has to be quoted AFAIR.

So this isn’t much about Unity but some absolute fundamental syntax rules of almost all programming languages.

2 Likes

As above. You have no idea how annoying it is for a 2D team to not be able to use “2D” for things like namespaces. This is why you see stuff like “U2D”. :wink:

2 Likes

:slight_smile:
2D would have so much issues. Technically it could be a hexadecimal number but actually it would be a double constant since the “d” and “D” suffix indicate a double literal. I recently read an article where someone had a very good argument against variables that are just numbers. Imagine someone creates a variable named 17 and sets it’s value to 42. What would this line of code do?

x = 3 + 4 * 17;

You think it would set x to 171? No, because you missed that “4” is also a variable that was set to 101 and 3 is also a string variable that is set to “Hello World”. So the correct string result would be “Hello World4242” :smile:

2 Likes

Pretty sure some early version of FORTRAN had this exact problem… some situation where you could assign into a numeric constant, something like 4 = 3;… and then any use of 4 would return 3… I never used any FORTRAN beyond the “hello world” point.

1 Like