Try to access variables of other script

LaneDetection LaneScript = GetComponent
In the above code LaneDetection is the script name
LaneScript is some random name.
I want to use the same script values in the other script. In the other script I have written the above code and trying to access the variables. But its giving errors. I did try of my own but could not get the answer. Can u tell me whats the error. If I write
if(LaneDetection.variablename == 1)
Error says :A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.GetComponent(System.Type)’

In C# if you want to access other variables in a script on the same GameObject you use

ScriptName exampleName =  gameObject.GetComponent<ScriptName>();
exampleName.variableName = newValue;

So for your LaneDetection script it’d be something like this:

LaneDetection laneScipt = gameObject.GetComponent<LaneDetection>();
laneScript.variableName = newValue;

There are other styles of doing this like:

LaneDetection laneScript = gameObject.GetComponent<LaneDetection>().variableName = newValue;

They do exactly the same thing. I prefer the first style more becuase then I can access multiple variables like so:

LaneDetection laneScript = gameObject.GetComponent<LaneDetection>();
laneScript.variable1 = newValue1();
laneScript.variable2 = newValue2();

In the script you’re referencing (in your case your LaneDetection script) you need to make sure the values are declared as public:

public type variableName;

Private variables don’t get found.

You can also call functions this way, replacing variableName with functionName. Referenced functions also need to be declared public.

If you’re looking for a way to call values and such from a GameObject that your scripts don’t share, GameObject.Find should be a good starting point:

LaneDetection laneScript = GameObject.Find("gameObjectName").GetComponent<LaneDetection>();
laneScript.variableName = newValue;

public static int variable; //This is decleration of variable in scriptA.

scriptA.variable = 1; //This is how to access it outside of scriptA.

There is a difference between a class and an object. A class is a type of an object but not a particular object. So to access object’s properties you need to have a reference to this object.

Try reading about Object-oriented programming, understand it’s basics and then look on Unity Scripting Lessons, they are awesome!

In order to keep things simple, assuming your LaneDetection class implementation: (pseudocode)

public class LaneDetection : MonoBehaviour    {
    public string publicIntanceVariable;
    static int myStaticVariable;
}

Now, Assume this script is attached to camera and another script is setting parameters:

LaneDetection LaneScript = Camera.main.GetComponent<LaneDetection> ();
LaneScript.publicIntanceVariable = yourStringValue; // Access variable through current instance
LaneDetection.myStaticVariable = youIntValue;       // Access variable by class name.

Notice the difference of LaneScript and LaneDetection in last two lines of code.

Hope it helps!

You can do this in this way also.

In your LaneDetection class declare variable like this:

public static LaneDetection Instance.

public int score;//for example

and assign Instance in awake like:

Awake(){
Instance = this;
}

Now you are ready to use this instance variable to get your public variable in other classes like this:

LaneDetection.Instance.score = 20;//or whatever value you want

Hope this will help you.