0

I am using Unity 5.3.5. I have a script that I wrote under the Unity Assets/MyScript folder.

ScriptA.cs

public class ScriptA : MonoBehaviour
{
    //code here
    public static bool flag_I_want_to_reference;
}

Then, because I am using the Unity standard asset vehicle/car thing, it is in a separate namespace in a different folder path (i.e. not in MyScripts folder but in a different path under Assets)

SomeCarScript.cs

namespace UnityStandardAssets.Vehicles.Car
{
    public class SomeCarScript : MonoBehaviour
    {
        //code here
        bool foo = ScriptA.flag_I_want_to_reference;
    }
}

Now, I have a bool that I want reference from within the SomeCarScript.cs but I am getting the error The name 'ScriptA' does not exist in the current context

I am trying to figure out what class/reference I need to have with using statements on the top of the SomeCarScript.cs script to make this work. I have tried looking up global namespaces and tried global::ScriptA.flag_I_want_to_reference and global::flag_I_want_to_reference but that doesn't work. I have also tried using "Assets/MyScripts/ScriptA.cs;" but that doesn't work either.

Any help would be greatly appreciated.

7
  • The only obvious thing is the wrong spelling of MonoBehaviour which should be pointed out by the compiler, but that should give you a Type or namespace not found compiler error. If you wrote the code directly into your question (that's what it looks like), that looks like an honest mistake. Commented Jul 25, 2016 at 15:42
  • @EvilTak right, that's what it was, it's not the real issue here but thanks for pointing it out. I'll edit the post. Commented Jul 25, 2016 at 16:06
  • Can you also tell us what directory SomeCarScript is in? Commented Jul 25, 2016 at 16:08
  • In the Unity editor, SomeCarScript is under Assets/StandardAssets/Vehicles/Cars/Scripts ScriptA is under Assets/MyScripts Commented Jul 25, 2016 at 16:44
  • That settles it. Look at my answer to this question here. Should solve your problem. Looks like the downvoter doesn't know what he's talking about :) Commented Jul 25, 2016 at 16:46

1 Answer 1

2

This looks like a compilation order issue.

If SomeCarScript is in a subdirectory of a "Standard Assets" folder and ScriptA is somewhere else, then it's because of the compilation order that ScriptA can't be referenced. ScriptA is compiled in a phase after SomeCarScript and therefore does not exist for SomeCarScript.

All code compiled in a specific phase can access code compiled in the same phase or an earlier phase, but not the code that will be compiled in a later phase (because, for the code being compiled, it doesn't exist yet).

General practice is to keep all code referencing the Standard Assets scripts in a folder other than "Standard Assets", so that it is compiled at a later stage and is able to access all other code.

Coming to your solution, you have two options:

  1. Move ScriptA to the same directory as SomeCarScript (not recommended)
  2. Move SomeCarScript to a directory which is not a subdirectory of "Standard Assets", "Pro Standard Assets" or "Plugins" (recommended)

Check out the Unity Manual page concerning script compilation order for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect. You are right. I moved my SomeCarScript from the Standard Assets subdirectories into where my ScriptA is located and everything compiled and worked for me.
Don't know why I got the downvote. Would love a comment on whatthe answer does wrong, even though it solved the OP's problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.