# 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>
|     |
|     |--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.

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
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.

Error is as follows:

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?


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

Any help is highly appreciated. Thanks in advance.

Updated above

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

Sample project:

https://ufile.io/knmxj

• Where is your Action.cs file located? – Sidar Jan 31 '18 at 18:32
• @Sidar Yes. Action.cs is attached as a component to FirstPersonCharacter and functions from that Action class are running properly. Also, there is only one FirstPersonCharacter object i.e. under FPSController object. Pardon me, I might not have understood you. – user62039 Jan 31 '18 at 18:35
• @Draco18s Sorry! That was a typo and I have also change the name Action.cs to Actions.cs, otherwise it is taking System.Action object. I have updated my question. Kindly wait! I am going to upload my sample project to make it clearer. – user62039 Jan 31 '18 at 18:48
• @Draco18s Hello! I have uploaded the project. Are you available to see it? – user62039 Jan 31 '18 at 19:04

# This is caused by Unity

But it's easy to fix.

The Standard Assets folder is compiled before all other scripts (this may be a holdover from when Unity officially supported JavaScript / UnityScript files, which had to be compiled at a different time than C# files, so the Standard Assets were compiled before everything else so that both C# and JS user created scripts could reference it).

All you have to do to fix this is change the name of the folder Standard Assets to anything else (except another one of the magic keyword names, like Resources).

Also, you have another error in your code:

public static bool is_acted;
...
GetComponent<Actions>().is_acted;


Your field is Static and cannot be accessed via an instance object. Either you should be doing Actions.is_acted` or remove the static modifier.

• Great! It is compiling now! Thanks a lot for the quick response! – user62039 Jan 31 '18 at 19:31
• @user62039 Glad I got it working for you. It was something I just had to play with once I had your project, as its a bizarre feature that isn't documented well/at all. – Draco18s Jan 31 '18 at 19:38