Search Unity

Education Can't change the name of c# scripts.

Discussion in 'Projects In Progress' started by silvergorilla, Feb 16, 2023.

  1. silvergorilla

    silvergorilla

    Joined:
    Feb 16, 2023
    Posts:
    2
    I was proceeding through the beginners tutorial and It wanted me to make a new script and rename it. If I renamed it and tried attaching it to one of my objects, It wouldn't work, If I left it unchanged, it would allow me to attach it. But I don't want a bunch of scripts that are all the same name. How do I fix this?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    You can rename the file in the Unity Editor, but then you have to open the script, and rename the class to match exactly the file name. After you have done this (assuming there are no compiler errors) you can attach the class to your objects.

    i.e. if you have filename MyCustomScript, the class should be like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MyCustomScript : MonoBehaviour
    5. {
    6. ...
    7. }
    You also need to make sure you have that ": MonoBehaviour" after your class declaration, so that your class derives from the base class MonoBehaviour. This is also needed to connect the script to Game Objects in the Editor.
     
    Last edited: Feb 16, 2023
    KadalAngop likes this.
  3. silvergorilla

    silvergorilla

    Joined:
    Feb 16, 2023
    Posts:
    2
    I was wrong. It's still not letting me. I went in and tried changing it manually. I named the script, and went into the actual programming and they were the same. But It's still not allowing me attach the script to an object. I've even tried making different ones with different names, and nothing.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Do you have the console window open in your Unity configuration? If not, open it (and always have it open if you didn't have it open.) Check what errors and warnings you might see there to get some clues what might be wrong.

    If you make a new script it should be definitely possible to attach it to an object. So either you are doing something wrong or there's something broken in your current Unity setup.