So I was following a Brackeys tutorial on how to make an inventory system, and when I saved one of the scripts, I noticed that it now said the script class cannot be found. I checked the script and there were no errors, and later I found out it made all the scripts have the same error message. When I remove the script everything works again. I am unsure of what is happening, so help would be nice.
Did you check the Console window in Unity? I don’t get to decide if there are errors in my scripts, the compiler does, and that’s where it’ll tell me.
Unity finds script classes by compiling your code, then looking inside each code file for a class which matches the filename.
So there are two potential points of failure: either the compilation can fail, or the engine can’t find a class with a name which matches the file name. If there is no error then the compile didn’t fail. That leaves you with option number two: your class name does not exactly match the name of the file.
In case you’re wondering, the reason the name needs to match is that it’s valid to have more than one class in a file. When a file has multiple classes in it Unity needs to know which one it should instantiate as a component, and that’s done based on the name.
There were no errors in the Console, and I double-checked the scripts and the class name matches the file.
Can you post the script? People will just be guessing if they can’t check anything. Perhaps also provide a link to the tutorial part that you are following in case there is something you have done differently from what is in the tutorial.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class BaseStat
{
public enum BaseStatType { Power, Toughness, AttackSpeed }
public List BaseAdditives { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public BaseStatType StatType { get; set; }
public int BaseValue { get; set; }
public string StatName { get; set; }
public string StatDescription { get; set; }
public int FinalValue { get; set; }
public BaseStat(int baseValue, string statName, string statDescription)
{
this.BaseAdditives = new List();
this.BaseValue = baseValue;
this.StatName = statName;
this.StatDescription = statDescription;
}
[Newtonsoft.Json.JsonConstructor]
public BaseStat(BaseStatType statType, int baseValue, string statName)
{
this.BaseAdditives = new List();
this.StatType = statType;
this.BaseValue = baseValue;
this.StatName = statName;
}
public void AddStatBonus(StatBonus statBonus)
{
this.BaseAdditives.Add(statBonus);
}
public void RemoveStatBonus(StatBonus statBonus)
{
this.BaseAdditives.Remove(BaseAdditives.Find(x => x.BonusValue == statBonus.BonusValue));
}
public int GetCalculatedStatValue()
{
this.FinalValue = 0;
this.BaseAdditives.ForEach(x => this.FinalValue += x.BonusValue);
this.FinalValue += BaseValue;
return FinalValue;
}
}
Video containing this specific script:
That script is a part of the inventory system, but the script is made in this video, not the actual inventory one