"Too many characters in character literal" in string list error C#

I have the following code:

private List <string> vowels = new List<string>();

void Start () {
    vowels = ['a', 'e', 'i', 'o', 'u', 'oo', 'ou', 'ea', 'iu', 'ia', 'ai', 'ae'];
}

I am only getting the error for strings with more than one character. From what I have seen on the forums and from my own logic, Unity thinks that this is a list of characters, not strings. How do I fix this?

Single quotes are for character (char) types. You want double quotes for string types.

7 Likes

I managed to solve the issue by replacing the single quotes with double quotes, but why does this work? :face_with_spiral_eyes:

Grant Thomas wrote this on Stack Overflow:

This is because, in C#, single quotes (‘’) denote (or encapsulate) a single character, whereas double quotes (“”) are used for a string of characters. For example:

var myChar = '=';

var myString = "==";
2 Likes

Ok, thanks! :slight_smile: Wrote the post before your reply came up.

It’s just how the compiler infers type with literals because they don’t inherently have meaning.

23
^ Is this an int, a float, a string? By default the compiler will say it’s an integer, the safest guess. But if we put an f after it, it becomes a float and if we wrap it in quotes, it becomes a string.

Probably a hold over from C/C++. Strings in C# are not null terminated, but in C/C++ they are. ‘a’ is a single character, where as “a” is a null terminated string. It’s actually two bytes, one for ‘a’ and one for ‘\0’.

2 Likes

Thanks for the information. I got really confused by this, because there is no difference in Python, my first language. :smile:

hello
can you help me please
this is my project
en utilisant System.Collections;
using System.Collections.Generic;
en utilisant UnityEngine;

public class WeaponManagerCZ805: MonoBehaviour {

public int bulletsPerMag = 31; // Nombres de balles par chargeurs
public int bulletsLeft = 310; // Notre réserve de balles
public int currentBullets; // Balles que l’on a

public float fireRate = 0,1f;
flotteur privé fireTimer;

public Transform shootPoint;

// Start est appelé avant la premiÚre mise à jour de la trame
void Start ()
{
currentBullets = bulletsPerMag;
}

// La mise à jour est appelée une fois par image
void Update ()
{
if (Input.GetButton (“Fire1”))
{
Feu();
}

if (fireTimer <fireRate)
fireTimer + = Time.deltaTime;
}

privé void Fire ()
{
if (fireTimer <fireRate) return;

RaycastHit a frappé;

if(Physics.Raycast(shootPoint.position, shootPoint.forward, out hit, range))
{
Debug.Log(hit.transform.name + ‘has been found’);
}

fireTimer = 0.0f;
}
}

1 Like

I am told that there is an error on line 42 word 44 I do not know what it is it is between the + and the ’

Hi, I don’t know how it’s fixed

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

public class PlayerMove : MonoBehaviour
{

public float runSpeed=2;

public float jumpSpeed = 3;

Rigidbody2D rb2D;

void Start()
{
rb2D=GetComponent();
}

void FixedUpdate()
{
if (‘Imput’.GetKey(“d”) || Imput.GetKey(“right”))
{
rb2D.velocity= new Vector2(runSpeed,rb2D.velocity.y);
}

else if (‘Imput’.GetKey(“a”) || Imput.GetKey(“left”))
{
rb2D.velocity= new Vector2(-runSpeed,rb2D.velocity.y);
}
else
{
rb2D.velocity=new Vector2 (0,rb2D.velocity.y);
}
}
}

1 Like

When u check if the player is preessing a key it’s:

if (Input.GetKeyDown(KeyCode.D))
{
what u want to do
}

1 Like

why it doesnt does its function??
public class CameraLook : MonoBehaviour
{

public float mouseSensitivity = 80f;

void Start()
{

}

// Update is called once per frame
void Update()
{

float mouseX = Input.GetAxis(‘Mouse X’) * mouseSensitivity * Time.deltaTime;

float mouseY = Input.GetAxis(‘Mouse Y’) * mouseSensitivity * Time.delta