script class can not be found unity error - c#

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.

Related

Unity Error error CS0117: 'Screen' does not contain a definition for 'fullscreen'

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.

Unity C# interfaces causing "script class cannot be found"

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

PlayerPrefs working on unity editor but not in build

I've looking for info to try to solve my problem and I just can't figure out what it's causing it. Player Prefs seem to work fine in unity editor, but once i make a build for Android or PC they are all gone. I have PlayerPrefs.deleteAll no where in my project.
I have 3 scenes: Menu, Game and GameOver. When I start the Menu scene, I run this script attached to the MainCamera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class prefs : MonoBehaviour
{
public static int bestRecord;
// Start is called before the first frame update
void Start()
{
bestRecord = PlayerPrefs.GetInt("K", 1);
Debug.Log(PlayerPrefs.GetInt("K").ToString());
}
}
When I go into the Game scene, my score is a TextMeshProUGUI that is constantly update by an InvokeRepeating:
public TextMeshProUGUI points;
void Start()
{
stop = false;
InvokeRepeating("subirMetros", 0f, 0.01f);
}
private void subirMetros()
{
if (stop == false)
{
points.SetText(Math.Round(character.transform.position.x, 0) + "");
}
}
//GameOver is called when the player dies
public void GameOver()
{
stop = true;
CancelInvoke("subirMetros");
int finalPoints = Int32.Parse(points.text);
int recordActual = PlayerPrefs.GetInt("K");
if (recordActual < finalPoints)
{
PlayerPrefs.SetInt("K", finalPoints);
}
SceneManager.LoadScene("GameOver");
}
}
Finally, when starting the GameOver scene, I get once again the PlayerPrefs that are supposedly saved before I get to the scene:
public TextMeshProUGUI record;
void Start()
{
record.text = PlayerPrefs.GetInt("K").ToString();
}
The weird thing is that this is working in the unity editor, but not when i build it. I would be very grateful if someone could help me, thanks.
I know this is an old post (forgive me for necro) but it came up as a google search result, and i ran into the same issue.
I had the same issue with playerprefs, i was saving an encrypted file where i used a playerprefs together with a generated key for a hash value. When i saved the game in the editor and ran in the build i couldnt load my save file. This was for windows though. After some debugging and digging I found out that unity saves playerprefs two different places for both editor and application builds:
according to this article
https://virtual-reality-piano.medium.com/solved-where-is-unitys-playerprefs-stored-on-windows-d61d504585e9
Your unity editor saves it at the path: ‘HKEY_CURRENT_USER\SOFTWARE\Unity\UnityEditor\CompanyName\ProjectName’
But, when you build your application, the player prefs are saved at:
'HKEY_CURRENT_USER\SOFTWARE\CompanyName\ProjectName' (where Company name is the name of your company, and project name is the name of your project).
I had to go in and delete the registry files in both places, because say i was saving a Save File with the name DefaultPlayer, then the editor would associate that save with the registry file from the editor. But when i build to an application, the save file would still persist in Application.PersistentPath, but it's registry file would not exist, because it wasn't created yet in the application's registry file.
Sorry for the confusing post and necro, but this is what fixed save file problems for me from editor to application builds. (note this is on windows so dont know if it applies to android as well). Hope it helps anyone else running into the same problem
note you would have to delete both files everytime you test in editor and on builds, or as someone suggested in a comment above, use the #if UNITY_EDITOR pragma to avoid the error
I read your post and I have a similar problem with this.
Before I have built an apk file but it was not working on the emulator.
But it was working on Unity Editor well.
I couldn't find the reason for this problem but I think there is a problem when the scene is loading using SceneManager.LoadScene().
When I don't use this function, the error was fixed.
If you fix this problem, please post how did you do.
And I think it is better to avoid errors than fixing errors.
Good luck.

Can't add script component 'HealthDisplay' because the script class cannot be found

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.

The type or namespace name `Kinect' does not exist in the namespace `Microsoft'

I am using Unity to develop a game and I want to use Kinect so I add a reference to Microsoft.Kinect.ddl and I have the following code (nothing impressive):
using UnityEngine;
using System.Collections;
using Microsoft.Kinect;
public class Main : MonoBehaviour {
// Use this for initialization
void Start () {
print(KinectSensor.KinectSensors.Count);
}
// Update is called once per frame
void Update () {
}
}
Visual Studio does not mark any error but when I am trying to run it using Unity I am getting a compilation error The type or namespace name Kinect does not exist in the namespace Microsoft . Any idea how can I fix that?
See this page for using the SDK with unity. It does have some obvious errors though (as pointed out in the documentation.)
I am using k2examples asset from the asset store, and I ran into the same problem... since you are not using the same asset, this might not solve the problem, but you can give it a try:
import the standard assets.

Categories

Resources