Can not drop CS script on game object

ERROR: Can’t add script component ‘DiceScript’ because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.

The import name is the same as the file name and same as the class name and they are all first letter capitalized (im not sure if this is a C# requirement) but it has not worked either way.

In unity asset window > Script
Script File name > Script.cs
public class Script : MonoBeaviour

The script is in the correct asset folder as well.

Do you have any compile errors in your Console window?

The things to check for first is to see if the name of the script ‘DiceScript’ in Unity is the same as the one in the visual studio, ’ public class DiceScript : MonoBehavior '. The second thing to do is to build in visual studio and seeing if the build is successful, and if it is then try and add the script to the game object. Sometimes if its a prefab that you’re trying to add the script to, it will ask if you want to open the prefab which if you do then you can add the script into the prefab directly.

Yes all the files name match, in my original post i neglected to remove ‘Dice’ from ‘DiceScript’ for simplicity.

I created a build for the file but its not building because it does not have access to >>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

are these deprecated?

After getting the SDK for C# I ran a build in the asset folder of my unity project and it seems like a few keywords classes or libraries are not accessible >>
UnityEngine,
MonoBehaviour,
Collider
Vector3

Im trying to model after this video in my attempt to learn game development. I dont know if this is an older version of unity I dont think he references the version. TY

https://www.youtube.com/watch?v=ouGJJzNPsSk

Please post a screenshot of your errors.

*dont mind the path displayed i edited it to remove certain folder names

Program.cs(3,7): error CS0246: The type or namespace name ‘UnityEngine’ could not be found (are you missing a using directive or an assembly reference?) [/Documents/Unity/New Unity Project/Assets/myApp/myApp.csproj]
Program.cs(5,31): error CS0246: The type or namespace name ‘MonoBehaviour’ could not be found (are you missing a using directive or an assembly reference?) [/Documents/Unity/New Unity Project/Assets/myApp/myApp.csproj]
Program.cs(15,24): error CS0246: The type or namespace name ‘Collider’ could not be found (are you missing a using directive or an assembly reference?) [/Documents/Unity/New Unity Project/Assets/myApp/myApp.csproj]
Program.cs(7,5): error CS0246: The type or namespace name ‘Vector3’ could not be found (are you missing a using directive or an assembly reference?) [/Documents/Unity/New Unity Project/Assets/myApp/myApp.csproj]

The build failed. Please fix the build errors and run again.

Thanks

An actual screenshot please.

1 Like

4985339--486299--Screen Shot 2019-09-20 at 2.52.45 PM.png

from terminal I cd into the app and run >> dotnet run
I tried to compile the c# code in visual studio code to see what the errors were to possibly explain why unity would not let me drop my script on a game object. TY

Building from terminal and dotnet run? Never heard of that approach myself. What happens when you choose Build from within Unity? The video that you shared does not use this approach. I might suggest getting the standard way of building working first.

1 Like