Arrays

I have just started programming and i was learning about arrays. In one of the exercises, when i tried assigning values to the array, it would give me this error:
error CS0029: Cannot implicitly convert type ‘(float, float, float, float, float, float, float, float, float, float, float, float)’ to ‘float[ ]’
This was my script:

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

public class ejerciciosDeArrays : MonoBehaviour
{
public float[ ] puntuacionesMaximas2 = new float[12];

// Start is called before the first frame update
void Start()
{
puntuacionesMaximas2 = (1.1f, 3.1f, 1.01f, 4.1f, 5.1f, 6.1f, 7.1f, 8.1f, 9.1f, 10.1f, 11.1f, 12.1f);
}

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

}
}

Thank you in advance for any replies.

please… CODE BLOCK

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

public class ejerciciosDeArrays : MonoBehaviour
{
    public float[] puntuacionesMaximas2 = new float[12];

// Start is called before the first frame update
    void Start()
    {
        puntuacionesMaximas2 = (1.1f, 3.1f, 1.01f, 4.1f, 5.1f, 6.1f, 7.1f, 8.1f, 9.1f, 10.1f, 11.1f, 12.1f);
    }
}

Classes should be capitalised too, not camel cased. objects are camel cased. its more identifyable.

if you read the error its telling you “I can’t convert your parameter FLOAT to type of FLOAT ARRAY”
to access the individual elements you could do a for-loop.

for arrays, specifically yours which is a single dimension, see here: Arrays - C# reference | Microsoft Learn

if you wanna set all, use

myArray = new type[count]{a, b, c}
2 Likes

It’s also not something you need to come to Unity to specifically ask; there’s endless resources online to find this info.

Typing “c# array” in Google gives you lots.

Here’s the first three listed which all immediately show you how to use them:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/

2 Likes