So far I am not clear how do you actually access the various components on a game object, to enable and disable them.

I have a component that has various scripts and elements, like a camera, transform, audio listener and few other custom scripts.

It is in the scene, so I try to access to it via script:

private GameObject _player;

_player = GameObject.Find("the_player") 

So far so good; if I want to access for example to the camera, and enable or disable it, I can do it

    _player.GetComponent<Camera>().enabled = true;

This disable correctly the camera, or eanble it, depending from the paramenter that I pass.

Now, if I want to access my script, called "playerscript"; I can't do the same thing, since I get an error:

    _player.GetComponent<playerscript>().enabled = true;

Not sure why I get an error; I have a class that is called "playerscript", that is attached to that _player object, but instead, VS complain.

This is the error message:

Error   CS0246  The type or namespace name 'playerscript' could not be found (are you missing a using directive or an assembly reference?)

I did try to use also GetComponentInChildren, but the result is the same.

If I have the object _player, which hold a reference to the original game object instantiated in the scene, why I can't simply access all the elements attached to it, via GetComponent and dot notation? I believe this used to work time ago, but in Unity5 I can't get this to work.

EDIT: This is the content of the playerscript class:

using UnityEngine;
using System.Collections;

public class playerscript : MonoBehaviour
    // Use this for initialization
    void Start()
        // get the instance of the button from UI manager

    void OnTriggerEnter(Collider world_item)
        //enable item animation

    void OnTriggerExit(Collider world_item)
        //disable item animation
  • 2
    \$\begingroup\$ What error you are getting? \$\endgroup\$ Feb 15, 2016 at 9:53
  • \$\begingroup\$ For safe side did you try to first turn on your script and get it? \$\endgroup\$ Feb 15, 2016 at 10:04
  • \$\begingroup\$ Are you sure they're in the same namespace or that you've imported the namespace playerscript is in? \$\endgroup\$
    – Sturlen
    Feb 15, 2016 at 11:39
  • 2
    \$\begingroup\$ What's the error message? playerscript needs to extend MonoBehaviour! \$\endgroup\$
    – ElDuderino
    Feb 15, 2016 at 11:46
  • 2
    \$\begingroup\$ Did you try to close solution in VS and reopen it manually? \$\endgroup\$ Feb 16, 2016 at 5:29

1 Answer 1


The solution came from Hamza's suggestion: close VS and start it again.

Once I did restart VS, now it is able to recognize correctly the scripts without problem.

I guess the issue is that VS get stuck sometimes, and does not see scripts created by the user, until you restart it. I will file a bug report to Microsoft to point out the issue.

If it ever happen to you; restart Unity and then launch VS double clicking on a script in Unity; which will restart VS.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .