Search Unity

Can't add Script

Discussion in 'Editor & General Support' started by games28, Jan 11, 2015.

  1. games28

    games28

    Joined:
    Dec 31, 2014
    Posts:
    32
    I’m writing the following code in the Editor:
    Using UnityEngine;
    Using System.Collections;
    Public class new: MonoBehaviou {
    Public int runspeed;
    //Use this for initialization
    Void Start () {
    }
    //Update is called once per frame
    Void Update () {
    }
    }
    When I try to attach the script to the main camera in Hierarchy view I get an error message saying: Can’t add script Can’t add component “new” because it doesn’t exist. Check to see if the file name and class name match.
    I'm learning Unity reading from a book. I don’t know what I’m doing wrong. Could you please help me?
     
  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    I haven't tried myself but I doubt you can name a class "new" since the Word "new" already is defined in C# as a keyword.
    Try changing the name of the class and see if it works, I Think your problem is that your code can't compile and therefore unity won't allow you to add the script to your gameobject. :)
     
  3. games28

    games28

    Joined:
    Dec 31, 2014
    Posts:
    32
    I've tried that. I changed the name of the class but it's still the same problem.
     
  4. marijnz

    marijnz

    Joined:
    Sep 20, 2012
    Posts:
    67
    Try copying the code inside of this class to notepad or something, then delete the whole file, create again and paste the code in it.
     
  5. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    The first thing you should do is make your code syntactically correct.
    ... otherwise it won't compile.

    Perhaps something like :
    Code (CSharp):
    1. // file: news.cs
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class news : MonoBehaviour
    7. {
    8.     public int runspeed;
    9.  
    10.     //Use this for initialization
    11.     private void Start() { }
    12.  
    13.     //Update is called once per frame
    14.     private void Update() { }
    15. }
    I assume your file name is the same as the class name??