Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access another script from FirstPersonController

sorry for my English!

I have scripts called "MYJoystick.cs":

 public class MYJoystick : MonoBehaviour 
 {
     public static string myX;
 }

and I want to access "myX", from "FirstPersonController.cs" in unity 5. I want to write this in the Update in the FirstPersonController Like this:

 private void Update()
 {
      MYJoystick.myX += 1;
 }

but it says "The name `MYJoystick' does not exist in the current context".

How can I fix this? should i add Using In FirstPersonController? using what? what is the default namespace in unity? I also tried to add Namespace for my script, but it didn't work too!

like image 499
ALI PAKKHOO Avatar asked Aug 11 '26 02:08

ALI PAKKHOO


1 Answers

I just had this problem.

http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

The basic rule is that anything that will be compiled in a phase after the current one cannot be referenced. The phases of compilation are as follows:

Phase 1: Runtime scripts in folders called Standard Assets, Pro Standard Assets and Plugins.

Phase 2: Editor scripts in folders called Editor that are anywhere inside top-level folders called Standard Assets, Pro Standard Assets and Plugins.

Phase 3: All other scripts that are not inside a folder called Editor.

Phase 4: All remaining scripts (ie, the ones that are inside a folder called Editor).

Your FPSController c# script is in the standard assets folder, this is compiled before your other custom c# scripts that are in another folder.

To fix this you could rename the 'Standard Assets' to another name, or put this folder into another folder or take the FPSController script and put this with your other scripts. A better solution would be to copy the FPSController standard assets script and put this into your custom folder and then modify this script instead.

I tried to change the compilation order of the scripts as a better solution, but it didn't seem to work. That's only for run time with Start() and Awake() I think between scripts.

like image 59
Martin Dawson Avatar answered Aug 13 '26 20:08

Martin Dawson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!