Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Tutorial problem: can't drag and drop script as component.

Discussion in 'Editor & General Support' started by Nanako, Sep 29, 2014.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Hi all. I'm currently doing this offficial tutorial:


    at about 1.28-1.35, the narrator creates a script, and then drags and drops it onto the add component panel. It is added as a component to the screenFader object

    however, when i attempt to do this, upon releasing the script, i get an error message instead

    Can't add script
    ------------------------
    Can't add component "SceneFadeInOut" because it doesn't exist, check to see if the filename and class name match

    I'm not certain what this error means in this context. Of course the script exists, i just created it. It also doesn't work with the two other scripts the tutorial has had me create thus far.

    What does work is using Add Component > New Script , CSharp, Create and Add. And then dragging my SceneFadeInOut script onto the component and replacing the empty new script i just created. This is cumbersome and inconvenient though. The way it's done in the video looks much simpler. Why isn't that working for me ?
     
    Wiikki likes this.
  2. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    Is the script you are referring to blank (as in, it has no content at all)?

    - If it is not, then check both the FILENAME and the CLASS NAME. That is to say, the name of the file has to match EXACTLY the word after the legend "PUBLIC CLASS" in the script.
    Pay SPECIAL ATTENTION to CaPiTaLiZaTiOn. For example, if you named your file "SceneFadeInOut" and you start the script with "public class Scenefadeinout"..., that will prompt the error you mention.

    - if it IS blank, then you may have left another script laying around somewhere in your project with the same name (and with the inconsistency I explained above) that is causing a problem. Delete that script if you can, and start over again.
     
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Nope, the script contains the default functions because i created it three seconds before attempting to use it, exactly as demonstrated in the video

    They are completely identical in every way. The script is named SceneFadeInOut.cs and the contents of the script are as follows

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SceneFadeInOut : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update () {
    13.  
    14.     }
    15. }

    I have attempted this several times. as well as saving and reloading the project, as well as creating and dropping on other scripts.I get the error mentioned in the first post whenever i attempt to drag and drop ANY script onto the add component panel of ANY object
     
  4. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    hmmm... that's weird. Let me check something:
    - Right click on your script and click "Show in Explorer"
    Post a screenshot here of the explorer window that opens up
     
  5. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
  6. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    Everything seems to be in order. I'm out of ideas.

    However, you do have at least one critical error on AlarmLight.cs... maybe fixing your errors will reveal the culprit.

    Clicking on the error line at the bottom will open the console where you can see all your errors. If there is more than one, you may want to post a screenshot of them as well.
    Sorry if I'm stating the obvious. Since you're doing a tutorial, I don't know how familiar you are with Unity.
     
  7. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    hi x
    yes I'm aware there are errors in that script, i got annoyed at inefficiency in the tutorial provided version and decided to rewrite it. Although i've not had the oportunity to compile or test it out yet, and i wrote my changes mostly on a loose knowledge of other programming languages, so i wouldn't be surprised to find that i messed up somewhere. I didn't think it'd be an issue right now.

    Here's my rewritten alarmLight, incase it's relevant. I can for now swap the default version back in and see if that helps..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AlarmLight : MonoBehaviour
    5. {
    6.  
    7.     public float fadeSpeed = 0.5f;//how many times per second the light will complete a full pulse from dark to light, or vice versa.
    8.     public float highIntensity = 2f;
    9.     public float lowIntensity = 0.5f;
    10.     public float changeMargin = 0.2f;
    11.     public bool alarmOn;
    12.  
    13.     private float targetIntensity;
    14.  
    15.     void Awake()
    16.     {
    17.         light.intensity = 0f;
    18.     }
    19.  
    20.     void Update()//Runs every frame as long as the alarm is on. Does NOT run when it's turned off
    21.     {
    22.         light.intensity = Mathf.Lerp (light.intensity, targetIntensity, fadeSpeed*Time.deltaTime);
    23.     }
    24.  
    25.     void checkTargetIntensity()
    26.     {
    27.         if (Mathf.Abs (targetIntensity-light.intensity) < changeMargin)
    28.         {
    29.             if (targetIntensity = highIntensity)//if we're pulsing up, we pulse down again, whether alarm is on or not.
    30.             {
    31.                 targetIntensity = lowIntensity;
    32.             }
    33.             else if (targetIntensity = lowIntensity && alarmOn)//If we're pulsing down, we pulse up again as long as the alarm is on.
    34.             {
    35.                 targetIntensity = highIntensity;
    36.             }
    37.             else//if we're pulsing down AND alarm is deactivated, then we set the intensity to zero and disable this script, effectively cleaning up
    38.             {
    39.                 targetIntensity = 0f;
    40.                 light.intensity = 0f;
    41.                 enabled = false;
    42.             }
    43.         }
    44.     }
    45.  
    46.     void alarmActivate()//call this function from an external source in order to start the alarm.
    47.     {
    48.         alarmOn = true;
    49.         enabled = true;
    50.         targetIntensity = highIntensity;
    51.     }
    52.  
    53.     void alarmDeactivate//Call this function from an external source in order to cause the alarm to stop AFTER the next pulse down. it will not cut out instantly.
    54.     {
    55.         alarmOn = false;
    56.     }
    57. }

    I'm not very familiar with unity, but i have a lot of other experience in coding and content generation and stuffs from other platforms, mostly i'm learning my way around the unity interface and how the pieces fit together, these tutorials aren't really teaching me new concepts, so much as unity's take on those concepts (which is still important)
     
  8. Razorwings18

    Razorwings18

    Joined:
    May 8, 2014
    Posts:
    69
    Well, at a quick glance, "void alarmDeactivate" is just missing the parentheses at the end, but that won't fix the issue of this thread.
     
  9. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    yeah it looks like i messed up a few logical operators too (= instead of ==) but it dopesn't seem to be related to my core problem. Would love anyone else to chime in. I hope this thread won't get ignored because of the number of replies it has, now. since I'm no closer to solving this problem D:
     
  10. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you have a compilation error, you can't expect anything to work regarding scripts. Always get rid of compilation errors first! Unity works with the compiled form of the scripts and if the compilation fails, it may not even have the script in a compiled form yet or an outdated one.
    When you have sorted out that issue and still can't assign the script, make sure the file name and the class name are matching.
     
  11. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    wow, it seems you were right. I've backed up and removed my efficient,. noncompiling script and thrown back in the tutorial one for now,. and things sem to be working. Thoroughly odd though.

    Why would this script not compiling, affect another? Why is it even being tested for compiling when i have not attempted to build or run the game yet, but am merely moving assets around and assigning references ?

    This is definitely going to come back and bite me in the ass again in future :(