I am using Unity 2019.3.13f1 and Visual Studio Community 2019 version 16.5.4. I have the script InterfaceContainer.cs as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InterfaceContainer : MonoBehaviour {
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
}
}
public interface IItem {
public string Name { get; }
public string Path { get; }
public GameObject Icon { get; }
public void Open();
}
Visual Studio gives no compilation errors.
In Unity the inspector of the script says "No MonoBehaviour scripts in the file, or their names do not match the file name." And when I drag the script into a GameObject, it says "Can't add script component "InterfaceContainer" because this script class cannot be found. Make sure that there are no compile errors and that the file name and the class name match."
The names definitely match, because when I deleted the interface part the error didn't exist anymore.
I also tried deleting the class part. It didn't help.
Any subsequent scripts added had the exact same error, whether or not they contain any interfaces, or reference this script.
The weird thing is when I deleted the interface part of this script, refreshed Unity, added this part again, and refreshed Unity again, the error disappears. However all subsequently added scripts still have the same error.
I have no idea what causes this error, and have googled for a long time with no avail. Any help will be greatly appreciated.
EDIT: The error isn't gone when I removed the interface part and added it again; I can still drag the script as a component, when when I try entering playmode it asks me to fix all compiler errors.
Try this:
Right click in the Project Panel of Unity
Import New Asset... and select InterfaceContainer.cs
Try add this script as component again.
One thing I would try is to check if it happens in earlier versions of unity. With the unity hub its easy to have different versions of unity to check this kind of things. Its useful if you are working with one of the latests.
I can confirm that in 2018.4.12, I attach my monobehaviours with interfaces with no problem.
On the other hand you dont implement the Interface you define in your monobehaviour, although that should not be causing any problem, have you tried if implementing the interface in your monobehaviour if you got the error?
Hope that helps
Related
I'm currently working on a game and I was making it's options menu and started scripting the fullscreen toggle. The code I've used for this can be found in a Brackeys tutorial but even after following whatever they did I've ran into this error:
Assets\Code\OptionsMenuScript.cs(44,16): error CS0117: 'Screen' does not contain a definition for 'fullscreen'
Most of the code in OptionsMenuScript.cs is useless in this question so I'll narrow it down to the important parts.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SwitchToFullscreen : MonoBehaviour
{
public void SetFullscreen(bool isFullscreen)
{
Screen.fullscreen = isFullscreen;
}
}
Even after looking in Project Settings several times I still haven't found a way to fix this. Am i supposed to define screen somewhere in my code? Is screen in the Project Settings but under a different name? Can anybody help me with this?
Also, if anyone needs it, I'm using Unity 2021.3.3f1 (3D Project).
public void SetFullscreen(bool isFullscreen) {
Screen.fullScreen = isFullscreen;
}
your Screen.fullScreen doesn't match with capitalization which I believe is throwing the errors. I tested using the code written above and received no errors in visual studio.
i'm new to unity and c# and after dealing with some compiling errors my script was compiling with just some warnings so i tried to put it into a game object and unity said "can't add script "startscreen" because script class can not be found" i searched about this error and all i found is about matching the class name and the file name but i checked so many times and it matches, after i saw about compiling errors but as i said the console only show warnings and finally i tried to delete all and recreate, i tried reimport all and i tried to change project now i realy don't know what to try, here's one of my 3 scripts maybe there is an error but the error massage appears on every script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class startscreen : MonoBehaviour
{
public event EventHandler switchMenu;
private void ChangeButtonSelected(object sender, EventArgs e){
buttonStart=gameobject.FindGameObjectWithTag("button").GetComponent<button1>();
buttonOther=gameobject.FindGameObjectWithTag("button").GetComponent<button2>();
butonStart.selectChange(e);
butonOther.selectChange(e);
}
void Start(){
switchMenu += ChangeButtonSelected;
}
void Update(){
if(input.GetKeyDown(KeyCode.Left) || input.GetKeyDown(KeyCode.Right)){
if(input.GetKeyDown(KeyCode.Left)){
string key = "left";
}if(input.GetKeyDown(KeyCode.Right)){
string key = "right";
}
switchMenu?.invoke(this, EventArgs.key);
}
}
}
I't very hard to solve the issue with the informations provided. I suggest you to recreate the scripts directly from Inspector Window -> Add Component -> Script component and check if it solve the issue.
I have seen similar questions, but I legitimately can't understand my problem here.
I have created a new script called HealthDisplay, from within Unity itself. It generated with some sample code, and I tried to attach it to a UI object that's text - however that error shows up.
I looked up the error and got all sorts of suggestions, however the code was generated by Unity itself, and the name of the class matched the name of the file exactly. I added some more code and tried to attach it then, but it still didn't work. I also tried reinstalling both Unity and the Unity Hub, and still nothing. Here's the code within HealthDisplay.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthDisplay : MonoBehaviour
{
private int health = 100;
public Text healthText;
// Update is called once per frame
void Update()
{
healthText.text = "HEALTH : " + health;
if(Input.GetKeyDown(KeyCode.Space))
{
health--;
}
}
}
The file, I've triple checked, is called HealthDisplay.cs. Its name matches exactly. Is there something I'm doing wrong, or could this just be some weird bug with Unity? Thanks.
so I'm new to Unity and I've been trying to test the scene with the script attatched to a character. However, it keeps saying "The associated script cannot be loaded. Please fix any compile errors and assign a valid script." It also says that the name of the file could be different from the name in the code but it isnt, and also it says that the code could be missing MonoBehaviour Scripts. It wont even allow me to attach the script to characters because it cant find the script class.
I've copied and downloaded character movement codes from the internet but they didnt work either. I've also tried deleting and re-making the CS files but that didnt work either. Even adding empty scripts to characters didnt work unless i do it from "Add component"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
SpriteRenderer sprite;
Rigidbody2D rigid;
// Start is called before the first frame update
void Start()
{
sprite = GetComponent<SpriteRenderer>();
rigid = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
if (Input.GetKey("d"))
rigid.velocity = new Vector2(2, 0);
else if (Input.GetKey("a"))
rigid.velocity = new Vector2(-2, 0);
}
}
There are also these errors in Unity if that helps
I think your class name is different from file name.
Unity apparently can't handle apostrophes (single-quote ') in the directory name of the editor. You need to get rid of the apostrophe in your directory name. Once you make that change, Unity should be able to build the scripts as intended.
Edit: This has been fixed in more recent versions - see https://issuetracker.unity3d.com/issues/scripts-do-not-get-compiled-if-the-unity-editor-path-contains-apostrophes for reference
First, it is recommended to use "Add component" to create a script, if you want to attach it to a GameObject, as it automatically imports necessary libraries. Implementing MonoBehaviour is necessary for adding a script to a GameObject.
Second, FixedUpdate() should not be set to private, it does not need an access modifier, just like Start(), see https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html.
Third, the errors in your first screenshot seem to imply that there is a problem with your Unity installation. Try reinstalling it and make sure that the Editor you install matches your operating system (64 or 32 bit?).
Fourth, the second screenshot is shown when you use any obsolete libraries or classes, which does not seem to be the case in the script you shared.
Hope that helps.
It's basically because you deleted some script or renamed it or degraded unity version. you might have to reassign the script at the required position/component.
Note: Make sure that class name is the same as the script name in unity.
[EDIT]
For someone who came across this page for some reason,
things have changed now. Starting Unity 2017, you have to define the same symbol in both files(Callee and Caller) regardless of the file location.
Below was the issue before Unity 2017.
I'll leave this for the record.
In standard C#, if I want to call a method with a conditional attribute, I have to define a symbol for it in the file where the method is CALLED. But in Unity, it seems different. It only works when I put the line in the file where the method EXISTS. Let's say, there are two script files in a Unity project like this.
ClassForMySymbol.cs
class ClassForMySymbol {
[System.Diagnostics.Conditional("MY_SYMBOL")]
public static void Print() {
Debug.Log ("This method is called.");
}
}
Test.cs
public class Test : MonoBehaviour {
void Start() {
ClassForMySymbol.Print();
}
}
This will only work when I define "MY_SYMBOL" in ClassForMySymbol.cs but not Test.cs.
I don't get what's happening. Is this normal? What am I missing here?
PS. This question is not about #define's scope or defining a global symbol.