Search Unity

Can not drop CS script on game object

Discussion in 'Scripting' started by owestjr, Sep 18, 2019.

  1. owestjr

    owestjr

    Joined:
    Sep 18, 2019
    Posts:
    5
    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.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Do you have any compile errors in your Console window?
     
  3. chevelle_wohr

    chevelle_wohr

    Joined:
    Sep 7, 2019
    Posts:
    15
    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.
     
  4. owestjr

    owestjr

    Joined:
    Sep 18, 2019
    Posts:
    5
    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?
     
    Last edited: Sep 18, 2019
  5. owestjr

    owestjr

    Joined:
    Sep 18, 2019
    Posts:
    5
    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
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please post a screenshot of your errors.
     
  7. owestjr

    owestjr

    Joined:
    Sep 18, 2019
    Posts:
    5
    *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
     
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    An actual screenshot please.
     
    Joe-Censored likes this.
  9. owestjr

    owestjr

    Joined:
    Sep 18, 2019
    Posts:
    5
    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
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    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.
     
    Joe-Censored likes this.