Почему unity не видит скрипт
Перейти к содержимому

Почему unity не видит скрипт

  • автор:


Почему unity не видит скрипт

In this article we look at the causes of the “Can’t Add Script” error in Unity in order to understand why it occurs and identify what actions to take to fix it.

Why do the “Can’t Add Script” Error occur in Unity?

This error in which we are not allowed to add a Script or component to a GameObject can arise for two reasons.

Case 1: The name of the script and the name of the class it defines do not match

The first one is that the name of the Script, that is to say, the name of the file with extension “.cs” does not match the name of the programming class that is defined inside it. This usually happens when we create a Script and then change the name of the file without doing a Refactoring process.

To solve this problem first we must identify the Script that has conflicts, this can be done with the information provided by the “Can’t Add Script” error window that appears when we try to add the Script with conflicts to a GameObject. If you do not remember which Script produced the error, you can create an Empty GameObject and start adding each of the Scripts until the error appears.

Once we know which is the Script that generates the “Can’t Add Script” error, we open it and we locate the part of the class definition, which begins as follows:

public class NameOfTheClass …

The name of the class is the name that has to match the name of the file, so what you can do is modify the name of the class to match the name of the file, or modify the name of the file to match the name of the class.

Once this problem is solved it will be possible to add the Script to a GameObject. If you need to rename the Script the best way is to right click on the “NameOfTheClass” name and then click on “Rename”, this will make Unity modify that name in all the places where that Script is used.

Case 2: The script was created having errors in the console

The second reason why the “Can’t Add Script” error occurs in Unity is when new Scripts are created while there are errors in console, in general these are syntax errors (errors in writing the code). The reason in this case is that if there are errors in console, Unity can not compile, therefore if we create a new Script at this point, even if the file has been created, the programming class can not be added to the engine, in other words Unity will not know that this Script exists until it compiles.

The solution in this case is to solve all the errors in the console, this will make Unity compile and register the new Script. From that point on you will be able to assign the Script to a GameObject.

Не работают скрипты в Unity3D.

public class Oblachko : MonoBehaviour

// Use this for initialization
void Start () transform.position = new Vector3(-12, 0.51f, -1f);


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

transform.Translate(new Vector2(0.1f, 0)*Time.deltaTime);
if (transform.position.x > 5)

Почему кнопка не видит функции моего скрипта Unity?

не видит функциюЗдравствуйте. Я создал кнопку и повесил на неё скрипт. Кнопка не видит функцию из моего скрипта. Модификатор доступа у функции стоит public. Что мне делать?введите сюда описание изображения

Знаете кого-то, кто может ответить? Поделитесь ссылкой на этот вопрос по почте, через Твиттер или Facebook.

Посмотрите другие вопросы с метками c# unity3d unity3d-ui или задайте свой вопрос.

Site design / logo © 2022 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2022.6.10.42345

How to fix issue where a Unity script is missing from Visual Studio but visible in Unity?

I have a problem regarding a Unity project I am currently working on. So far, everything went fine and I have been able to add 6 C# scripts to my project. However, I have now run into a problem. I can still create a new script ("Timer") in the Unity interface: View in Unity

However, this script does not show up in the script "hierarchy" (I am using VIsual Studio):

I do not know why this is the case. The name of the script and of the class are the same. The script compiles without error. I can even attach the script to GameObjects and it does what it is supposed to do. However, other scripts don’t have access to said script which I suppose is because the "Timer" script is not connected to the other scripts.

I would appreciate any help.

2 Answers 2

This is a common problem.

  1. Close Visual Studio
  2. Delete all *.csproj and .sln files from your Unity project folder (the one containing Assets, Packages etc)
  3. In Unity, choose Edit > Preferences > External Tools
  4. Click Regenerate project files
  5. In the Unity Project window, double-click one of your scripts. Unity automatically regenerates the solution file along with all project files

Visual Studio opens showing your solution, your projects and scripts.

Visual studio uses a so called "project" file to keep track of which classes / scripts are being used together. This project file (in this case it’s "Assembly-CSharp", where your self-made scripts are stored in) is what Visual Studio uses and it is not the same as your Unity project! It seems like Unity didn’t add your Script to the C# project automatically, which it usually would.

(This could be a sign of a broken project, so I would recommend going with MickyD’s solution instead of mine, as their solution is likely to prevent future incidents of this as well!)


Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *