Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Script causing all scripts to say "Script class cannot be found"

Discussion in 'Scripting' started by Archxr_10k, Jun 6, 2022.

  1. Archxr_10k

    Archxr_10k

    Joined:
    Mar 19, 2022
    Posts:
    4
    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.
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    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.
     
  3. Archxr_10k

    Archxr_10k

    Joined:
    Mar 19, 2022
    Posts:
    4
    There were no errors in the Console, and I double-checked the scripts and the class name matches the file.



     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    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.
     
  5. Archxr_10k

    Archxr_10k

    Joined:
    Mar 19, 2022
    Posts:
    4
    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<StatBonus> 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<StatBonus>();
    this.BaseValue = baseValue;
    this.StatName = statName;
    this.StatDescription = statDescription;
    }

    [Newtonsoft.Json.JsonConstructor]
    public BaseStat(BaseStatType statType, int baseValue, string statName)
    {
    this.BaseAdditives = new List<StatBonus>();
    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:
     
  6. Archxr_10k

    Archxr_10k

    Joined:
    Mar 19, 2022
    Posts:
    4
    That script is a part of the inventory system, but the script is made in this video, not the actual inventory one