Search Unity

can't insert script to object.

Discussion in 'Scripting' started by dhildrew, May 26, 2020.

  1. dhildrew

    dhildrew

    Joined:
    May 26, 2020
    Posts:
    7
    Hello. Im trying to insert a script to an object (its rotation control for the mouse). I keep getting a message saying the script class cannot be found.

    Ive tried restarting unity, i have no compiler errors, ive copied the script into a new script file.

    I'm fairly new to game engines so I would really appreciate some help. Thanks!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Is the script filename the exact same as the name of the class?

    Screenshots and copy/paste of the exact error text may help.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Usually if it says a script can't be found it's because the script filename and the class name in the script itself don't match.
     
  4. dhildrew

    dhildrew

    Joined:
    May 26, 2020
    Posts:
    7
    I'm not sure what that means? Filename hasn't changed.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I'm not sure how we can phrase it any different.

    If your class name is "ThisIsAnAwesomeScript"
    Then your script filename must also be "ThisIsAnAwesomeScript"

    Letter for letter, caps matter, etc. Also, no spaces.

    You also don't want to try to put multiple MonoBehavior scripts under the same file. It doesn't work that well either if you are trying that. (not saying you are, just pointing that out as well.)
     
  6. dhildrew

    dhildrew

    Joined:
    May 26, 2020
    Posts:
    7
    Thanks for your help. Ive got the script working now. i can insert it to the object. however it doesn't seem to work when testing. its to control the object with the mouse. Is this correct? Thanks again for your help.



    {

    float rotationSpeed = 2f;

    void OnMouseDrag()
    {
    float XaxisRotation = Input.GetAxis("Mouse X") * rotationSpeed;
    float YaxisRotation = Input.GetAxis("Mouse Y") * rotationSpeed;

    transform.Rotate(Vector3.down, XaxisRotation);
    transform.Rotate(Vector3.right, YaxisRotation);
    }
    }
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you familiar with Debug.Log? Add debugging to figure out where the problem is occurring. It could be OnMouseDrag() is not even being called. It could be X/YaxisRotation are not values you expect. It could be you don't have axis with those specific names defined in the Input Manager (is the plural of "axis" also just axis? axii? axises? :p ). It could be the script is attached to the wrong gameobject, making transform a reference to again the wrong gameobject's transform. It could be any number of things.
     
  8. dhildrew

    dhildrew

    Joined:
    May 26, 2020
    Posts:
    7
    I am aware of it but never used it. i believe the the plurals are the same "axis". ive tried another script that works perfect now. however the mouse control inverts when i switch to iether half of the screen which makes it tricky to move around the object. thankyou for your help so far. if you have a suggestion for solving the inversion that would be great otherwise dont worry.
     
  9. dhildrew

    dhildrew

    Joined:
    May 26, 2020
    Posts:
    7
    {
    public float rotationSpeed = 10;

    public Transform cam;


    void Update()
    {

    RotateObject();
    }


    void RotateObject()
    {
    transform.Rotate(cam.up, Input.GetAxis("Mouse X") * rotationSpeed, Space.World);

    transform.Rotate(cam.forward, Input.GetAxis("Mouse Y") * rotationSpeed, Space.World);

    }

    }