Skip to main content
added 108 characters in body
Source Link

Sample project:

https://ufile.io/knmxj

Sample project:

https://ufile.io/knmxj

added 108 characters in body
Source Link
<Assets>
  |
  |--<Scripts>
  |     |
  |     |--ActionActions.cs
  |
  |--<Standard Assets>
        |
        |--<Characters>
              |
              |--<FirstPersonCharacter>
                   |
                   |--<Scripts>
                         |
                         |--FirstPersonController.cs

Now FirstPersonController.cs is attached to FPSController by default and ActionActions.cs is attached by me to FirstPersonCharacter.

...
namespace UnityStandardAssets.Characters.FirstPerson
{    
     ...
     public class FirstPersonController : MonoBehaviour
     {
          ...
          private void GetCharacter()
          {    
               GameObject _obj = GameObject.Find("FirstPersonCharacter");// ok here
               Debug.Log(bool is_acted = _obj.GetComponent<Action>GetComponent<Actions>();.is_acted; // error here, please see below
          }
          ...
     }
}
        

Excerpt from ActionActions.cs is as follows:

public class ActionActions: MonoBehaviour {
    ...
    public static bool is_acted;
    ...
}

All I want is to access bool is_acted variable of ActionActions.cs from FirstPersonController.cs.

Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/ 
FirstPersonController.cs(16663,6738):error CS0246: The type or namespace
name `Action'`Actions' could not be found. Are you missing an assembly reference?

Any help is highly appreciated. Thanks in advance.

Updated above

  • Changed Action to Actions.
  • Corrected typo ().
<Assets>
  |
  |--<Scripts>
  |     |
  |     |--Action.cs
  |
  |--<Standard Assets>
        |
        |--<Characters>
              |
              |--<FirstPersonCharacter>
                   |
                   |--<Scripts>
                         |
                         |--FirstPersonController.cs

Now FirstPersonController.cs is attached to FPSController by default and Action.cs is attached by me to FirstPersonCharacter.

...
namespace UnityStandardAssets.Characters.FirstPerson
{    
     ...
     public class FirstPersonController : MonoBehaviour
     {
          ...
          private void GetCharacter()
          {    
               GameObject _obj = GameObject.Find("FirstPersonCharacter");// ok here
               Debug.Log(_obj.GetComponent<Action>); // error here, please see below
          }
          ...
     }
}
        

Excerpt from Action.cs is as follows:

public class Action: MonoBehaviour {
    ...
    public static bool is_acted;
    ...
}

All I want is to access bool is_acted variable of Action.cs from FirstPersonController.cs.

Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/ 
FirstPersonController.cs(166,67):error CS0246: The type or namespace
name `Action' could not be found. Are you missing an assembly reference?

Any help is highly appreciated. Thanks in advance.

<Assets>
  |
  |--<Scripts>
  |     |
  |     |--Actions.cs
  |
  |--<Standard Assets>
        |
        |--<Characters>
              |
              |--<FirstPersonCharacter>
                   |
                   |--<Scripts>
                         |
                         |--FirstPersonController.cs

Now FirstPersonController.cs is attached to FPSController by default and Actions.cs is attached by me to FirstPersonCharacter.

...
namespace UnityStandardAssets.Characters.FirstPerson
{    
     ...
     public class FirstPersonController : MonoBehaviour
     {
          ...
          private void GetCharacter()
          {    
               GameObject _obj = GameObject.Find("FirstPersonCharacter");// ok here
               bool is_acted = _obj.GetComponent<Actions>().is_acted; // error here, please see below
          }
          ...
     }
}
        

Excerpt from Actions.cs is as follows:

public class Actions: MonoBehaviour {
    ...
    public static bool is_acted;
    ...
}

All I want is to access bool is_acted variable of Actions.cs from FirstPersonController.cs.

Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/ 
FirstPersonController.cs(63,38):error CS0246: The type or namespace
name `Actions' could not be found. Are you missing an assembly reference?

Any help is highly appreciated. Thanks in advance.

Updated above

  • Changed Action to Actions.
  • Corrected typo ().
Source Link

Unity: Cannot access variable of C# script from another object

I have already found similar questions on stackexchange.com related to accessing variables from another object. But my question seems to have a slightly different context and the answers from those questions are not working.

I have the following (stripped off) scene comprising the standard FPS character:

Scene001
  |
  |--FPSController
       |
       |--FirstPersonCharacter

My (stripped off) Assets hierarchy is as follows (folders are enclosed with <> brackets):

<Assets>
  |
  |--<Scripts>
  |     |
  |     |--Action.cs
  |
  |--<Standard Assets>
        |
        |--<Characters>
              |
              |--<FirstPersonCharacter>
                   |
                   |--<Scripts>
                         |
                         |--FirstPersonController.cs

Now FirstPersonController.cs is attached to FPSController by default and Action.cs is attached by me to FirstPersonCharacter.

Excerpt from FirstPersonController.cs is as follows:

...
namespace UnityStandardAssets.Characters.FirstPerson
{    
     ...
     public class FirstPersonController : MonoBehaviour
     {
          ...
          private void GetCharacter()
          {    
               GameObject _obj = GameObject.Find("FirstPersonCharacter");// ok here
               Debug.Log(_obj.GetComponent<Action>); // error here, please see below
          }
          ...
     }
}
        

Excerpt from Action.cs is as follows:

public class Action: MonoBehaviour {
    ...
    public static bool is_acted;
    ...
}

All I want is to access bool is_acted variable of Action.cs from FirstPersonController.cs.

Error is as follows:

Assets/Standard Assets/Characters/FirstPersonCharacter/Scripts/ 
FirstPersonController.cs(166,67):error CS0246: The type or namespace
name `Action' could not be found. Are you missing an assembly reference?

To me this seems to be a problem of namespace or folder location!!?

Any help is highly appreciated. Thanks in advance.