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

"No MonoBehaviour scripts in the file" not the case with my code? Please help!

Discussion in 'Editor & General Support' started by Balthasaurus, Dec 11, 2018.

  1. Balthasaurus

    Balthasaurus

    Joined:
    Dec 11, 2018
    Posts:
    5
    Hello everyone!

    I'm relatively new to programming with unity and I'm having a little problem with one of my c# scripts. I realise that it is a common error to see the error message "No MonoBehaviour scripts in the file, or their names do not match the file name." However, this does not appear to be the case. As a result, I cannot get my camera to follow my player.

    My code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraController : MonoBehaviour {
    6.  
    7.     public Transform target;
    8.  
    9.     public Vector3 offset;
    10.  
    11.     public float pitch = 2f;
    12.  
    13.     private float currentZoom = 10f;
    14.  
    15.     private void LateUpdate()
    16.     {
    17.         transform.position = target.position - offset * currentZoom;
    18.         transform.LookAt(target.position + Vector3.up * pitch);
    19.     }
    20. }
    I am certain my script is called CameraController and is in the correct folder, so what could be the problem?

    I'll upload a screenshot as well.

    If anyone can recognise the problem I'm having and suggest a solution, that would be much appreciated.
     

    Attached Files:

  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    Don't you have a space between the CameraController and the .cs in the filename? It's hard to tell on the screenshot but looks like it.
     
  3. Balthasaurus

    Balthasaurus

    Joined:
    Dec 11, 2018
    Posts:
    5
    I see the confusion and tested it myself but there is no space there. Thanks for trying to help so rapidly!
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    Try to save your changes and try to attach it to a game object. Do you have other exceptions/errors in the console (in this case even a warning?)
     
  5. Balthasaurus

    Balthasaurus

    Joined:
    Dec 11, 2018
    Posts:
    5
    I have, but I think I've resolved them and they aren't seemingly relevant to my current problem. I think I have discovered the cause, however. The script was already attached to the Main Camera but it seems that was done incorrectly. I cannot work out how to fix this. I'll send two screenshots, one of the console and another of what seems to be the root of the problem.
     

    Attached Files:

  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,904
    Fix your PlayerController first, it probably will fix the other issue too.

    If not, we can go from there. :D
     
  7. Balthasaurus

    Balthasaurus

    Joined:
    Dec 11, 2018
    Posts:
    5
    Okay....
    Well, it seems a stray comma before closing a bracket was the source of my error with the PlayerController, though this was not apparent as the game was running and I didn't understand how it could bug another script. Removed it and now the CameraController looks fine! Thanks for your help, and it was good to see a Discworld fan help me with my coding!
     

    Attached Files:

    Lurking-Ninja likes this.