Calling C# procedures from C# procedure separate cs files

Hi, firstly I say that I have tried many variations in order to get this to work nd have no luck. Have searched the forums until I was blue in the face. Can someone please help with this niggle… please.

In the scene I have created an empty, to which several C# scripts are attached. They are all located in the same directory. The main C# Class calls the other public class scripts and the relevant procedures located in each.

The structure is represented below as the first Script calling a function in the the second Script/Class which needs to return a value to the first.

What am I missing or doing wrong. Your help would be greatly appreciated.

First cs file

public class PlayerMenu : MonoBehaviour {
....

void OnGUI () {
...
       if(CheckValidInput.CheckValidInputFromX(BoardArray.MoveFromX)){
			GUI.Button(new Rect(10,200,240,20), "Execute Move", GUI.skin.button);
       }
}

Second cs file looks something like this.

public class CheckValidInput : MonoBehaviour {
....
	public bool CheckValidInputFromX(string MoveFromX){
				
				int x = 0;
				bool ValidInputFromX = false;
	
				switch(MoveFromX){
				case "a":
					x = 1;
					break;
				case "h":
					x = 8;
					break;
				default:
					x=0;
					break;
				}
				if((x>0)  (x<9)){
					ValidInputFromX = true;
				}
				else
				{
					ValidInputFromX = false;
				}
				
				return ValidInputFromX;
	}
....
}

Many Thanks

Your first script it referencing the second script, but you’re not telling it where to find it.

Do this:

In your first script, add

public CheckValidInput checkValidInput;

and use this new variable in your script:

void OnGUI () { 
... 
       if(checkValidInput.CheckValidInputFromX(BoardArray.MoveFromX)){
         GUI.Button(new Rect(10,200,240,20), "Execute Move", GUI.skin.button); 
       }

Note the lowercase of “checkValidInput”. We’re using the public variable you created at the top of this script, not the actual mono behavior class.

Now, in the inspector for where Script 1 is attached, you will see a “CheckValidInput” variable for you to drag the second script to (or select from dropdown menu). This will connect your second script to your first.[/code]

Better yet make the methods in the auxiliary classes static and calls in the main class such as CheckValidInput.CheckValidInputFromX() should work fine.