C# Multiple script errors

Can’t fix this damn script for almost 2 weeks. IDK what’s wrong with it. Can somebody Help please?


using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Manager : MonoBehaviour
{
  public GameObject vfxSource;
  public AudioSource[] efxSourceArray;
  private int currentSource;
  private AudioSource efxSource;
  public AudioSource musicSource;
  public AudioSource sinLoopSource;
  public AudioSource sinSource;
  public AudioSource amdSource;
  public static Manager instance;
  public float lowPitchRange = 0.95f;
  public float highPitchRange = 1.05f;
  public bool playerTurn;
  public string directory;
  public string[] menuTxt;
  public string[] dlcTxt;
  public bool deadSound;
  public float mute = 1f;
  public bool doorsClosing;
  public bool machineStatus;
  public bool first = true;
  public int AMDphase;
  public bool dlcTransition;
  public bool amdStatus;
  public AudioClip amdLoopTrack;
  public AudioClip sinLoopTrack;
  public bool steam;

    private void Awake()
  {
    if ((Object) Manager.instance == (Object) null)
      Manager.instance = this;
    else if ((Object) Manager.instance != (Object) this)
      Object.Destroy((Object) this.gameObject);
    Object.DontDestroyOnLoad((Object) this.gameObject);
    Cursor.visible = false;
    this.directory = Directory.GetCurrentDirectory();
    this.menuTxt = File.ReadAllLines(this.directory + "/local/m.json");
    this.dlcTxt = File.ReadAllLines(this.directory + "/localHM/hm_m.json");
    this.VolumeChange(0, PlayerPrefs.GetInt("musicVol", 2));
    this.VolumeChange(1, PlayerPrefs.GetInt("efxVol", 3));
  }

     public void PlaySingle(AudioClip clip)
  {
    this.efxSource.clip = clip;
    this.efxSource.Play();
  }

     public void RandomizeSfx(bool pitchBool = true, params AudioClip[] clips)
  {
    if (this.deadSound && !this.doorsClosing)
      return;
    this.doorsClosing = false;
    this.efxSource = this.efxSourceArray[this.currentSource];
    ++this.currentSource;
    if (this.currentSource == this.efxSourceArray.Length)
      this.currentSource = 0;
    int index = UnityEngine.Random.Range(0, clips.Length);
    this.efxSource.pitch = !pitchBool ? 1f : UnityEngine.Random.Range(this.lowPitchRange, this.highPitchRange);
    this.efxSource.clip = clips[index];
    this.efxSource.Play();
  }

  public void PlaySin(AudioClip sinClip)
  {
    if (this.deadSound)
      return;
    this.StartCoroutine(this.SinChanger());
    this.sinSource.clip = sinClip;
    this.sinSource.Play();
  }

     public void RandomizeVfx(GameObject[] clips, Vector3 place, bool random)
  {
    int index = UnityEngine.Random.Range(0, clips.Length);
    this.vfxSource = clips[index];
    if (random)
      place = new Vector3(place.x + UnityEngine.Random.Range(-0.1f, 0.1f), place.y + UnityEngine.Random.Range(-0.1f, 0.1f), place.z);
    UnityEngine.Object.Instantiate<GameObject>(this.vfxSource, place, this.vfxSource.transform.rotation);
  }

  public void VolumeChange(int type, int lvl)
  {
    float num = (float) lvl / 3f * this.mute;
    if (lvl == 2 || lvl == 1)
      num += 0.04f;
    if (type == 1)
    {
      this.sinLoopSource.volume = num;
      this.sinSource.volume = num;
      this.amdSource.volume = num;
        for (int index = 0; index < this.efxSourceArray.Length; ++index)
        this.efxSourceArray[index].volume = num;
      PlayerPrefs.SetInt("efxVol", lvl);
    }
    else
    {
      this.musicSource.volume = num;
      PlayerPrefs.SetInt("musicVol", lvl);
    }
  }

     public IEnumerator SongChanger(AudioClip track)
  {
    float tempVol = this.musicSource.volume;
    while ((double) this.musicSource.volume > 0.0)
    {
      this.musicSource.volume -= 0.04f;
      yield return (object) new WaitForSeconds(0.01f);
    }
    this.musicSource.volume = tempVol;
    this.musicSource.clip = track;
    this.musicSource.Play();
  }

    [IteratorStateMachine(typeof(Manager.<SinChanger>d__35))]
    public IEnumerator SinChanger()
    {
        while (true)
        {
            int num;
            switch (num)
            {
            case 0:
                if (!this.machineStatus)
                {
                    this.machineStatus = true;
                    this.sinLoopSource.mute = false;
                    yield return new WaitForSeconds(0.1f);
                    continue;
                }
                this.machineStatus = false;
                this.sinLoopSource.mute = true;
                yield return new WaitForSeconds(0.1f);
                continue;
            case 1:
                goto IL_5D;
            case 2:
                goto IL_92;
            }
            break;
        }
        yield break;
        IL_5D:
        IL_92:
        yield break;
    }

  public void PlayAMD(AudioClip amdClip)
  {
    if (this.deadSound)
      return;
    this.sinSource.clip = amdClip;
    this.sinSource.Play();
  }

      public void PlayAMD(AudioClip amdClip)
  {
    if (this.deadSound)
      return;
    this.sinSource.clip = amdClip;
    this.sinSource.Play();
  }

  public void PlayHands(AudioClip handClip)
  {
    if (this.deadSound)
      return;
    this.amdSource.clip = handClip;
    this.amdSource.Play();
  }

  public void LoopAMD()
  {
    this.sinLoopSource.clip = this.amdLoopTrack;
    this.sinLoopSource.Play();
    this.sinLoopSource.mute = false;
    this.amdStatus = true;
  }

  public void LoopMute()
  {
    this.sinLoopSource.clip = this.sinLoopTrack;
    this.sinLoopSource.Play();
    this.sinLoopSource.mute = true;
    this.amdStatus = false;
  }
}

Just start Visual Studio and do what the compiler says. You are casting on (Object) which is ambiguous in Unity game objects. You have a duplicate PlayAMD method, you are using unassign int in switch… So probably the rest of the errors are also either syntactical or have no reason to exist in Unity.

I read all this only from running the code in Visual Studio and reading the errors after hovering the cursor.

Don’t guess at errors, just read the messages.

All the above errors look like fat-finger typing mistakes. You can fix those all by yourself in seconds, here’s how:

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Finally, when you have errors, don’t post here… just go fix your errors! See the top of this post.

line 126: [IteratorStateMachine(typeof(Manager.d__35))]

This stuff triggers your errors. I’m not very familiar with enumerators but it looks to me that your return type should be IEnumerable according to this: Muhammad Azeez - How the C# yield keyword works, or you could try to remove the attribute line.

I’m sorry but is this part of a comedy routine? There are 5 posted images, to me they all look identical. This to display 3 errors which could have been cut and paste into a message.

As was pointed out there is a switch statement based upon a local variable defined right above it, it can’t have any value but zero can it? While that could be valid does anything ever change that value?

Now here are the comedy bits…

Did someone write this? Who casts null to an Object?

    private void Awake()
  {
    if ((Object) Manager.instance == (Object) null)
      Manager.instance = this;
    else if ((Object) Manager.instance != (Object) this)
      Object.Destroy((Object) this.gameObject);

And then we decided to write in IL?

yield break;
        IL_5D:
        IL_92:
        yield break;

I think it might all be auto-genned code?? This weird decorator for instance:

    [IteratorStateMachine(typeof(Manager.<SinChanger>d__35))]

I’ve never seen anything like that in Unity and the d__35 identifier really smells like auto-genned code.

Either way the code looks super-terse, like packed with dense lines… not exactly beginner code. Hm.

1 Like

Thinking a little more on the above posts, perhaps what you should tell us is what YOU think this script should do, and why you think it should do that.

Otherwise I think OP got ahold of some code that is a tiny fraction of a much-larger and more-complex Unity project.