Need some help with a C# script, new to C#

Hi,

Below is my script for rotating a game object according to angles given by a text-file. My thought is to read the text-file in the void Start() and store it into an array. My script is below and the error messages down under.

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using UnityEngine;



var fileName = "azimuthAngles.txt";
float[] azimuth;

public class Rotate : MonoBehaviour
{
  void Start()
     {
         var sr = new StreamReader("C: \\Users\\linnl\\OneDrive - NTNU\\Marin5autumn\\AR - Project" + "/" + fileName);

         var fileContents = sr.ReadToEnd();
         sr.Close();
         var lines = fileContents.Split("\n"[0]);

         int i;
         foreach (string line in lines)
         {
             azimuth[i] = Convert.ToSingle(line);
             i += 1;
         }
    //transform.RotateAround(transform.position, Vector3.up, azimuth[0] );
}
    void Update()
    {

        /int elapsedTime_int = (int)(Time.time);
        transform.RotateAround(transform.position, Vector3.up, azimuth(elapsedTime_int * 15 + 2));
      
    }
}
[\code]

Your line numbers are a bit off. I’m not sure where you are missing a semicolon in the first error. Please try to make them fit between the error messages and your posted code example. For now here are a couple fixes (line numbers for current example):

  • In line 34 you wrote /int. Remove the slash there.
  • In line 35 you are writing azimuth(). Your azimuth, as declared in line 11, is an array tho, so you probably meant to write azimuth[ ], like you did in like 26. Otherwise the compiler thinks azimuth is supposed to be a method.

Some of the other errors may be follow up errors by the confused compiler. So fix those two and report back.

Also, especially as a beginner i would consider not using ‘var’ for everything. Fill in the proper types. That makes debugging and learning a lot easier. Everything is strongly typed in C# but ‘var’ just kinda makes you not think about types. I personally rarely use ‘var’ and mostly as a convenience tool, when type names got very long and i want to improve readability.

1 Like

In addition to what @Yoreki said:

Change ‘var’ on line 10 to string.

Move line 10 and 11 inside the class.

add the following to your using directives at the top.

using System;

You need it for Convert on line 26.

You’ll need to assign i to a value on line 23 as well. Right now, you’ve only declared it.

1 Like

I have no idea how i missed that haha :smile:

1 Like

It’s fine, I didn’t spot it until I cheated and copy/pasted it in to VS :slight_smile:

2 Likes

Thanks, you guys are amazing! There are more problems in this script but at least it is free of error :slight_smile:

3 Likes

I’d recommend not declaring all your variables as “var”, as it makes your code less readable. Yes I know the type can be determined by the context, but still less readable especially when you’re getting someone else’s help with it. My 2 cents

2 Likes