How can I check a variable in one script from another one in c#?

ok I asked this before but I posted two very massive script files. I figured that might be intimidating to read so I shortened it :slight_smile: isstunned is a variable from the file I am trying to access it’s name is PlayerScript it’s component is Player Script and it’s public class is PlayerScript

	void Update () 
{
		Pscript = GetComponent<PlayerScript>();
		if(Pscript.isstunned == 0)
		{
			//if a button is pressed
			if(Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
			{
				//set row of sprite sheet to 1
				rownum = 1;

…etc etc

btw this is the compile error

Assets/Scripts/PlayerAnimation.cs(23,17):
error CS0103: The name `Pscript’ does
not exist in the current context

You need to mark the variable as public

For instance (this is not a unity example, mearly to show how to share variables between classes)

class ClassOne {
  public int Number = 34; // A number that's public
  private string Text = "pi = 3.14159265..."; // some text that's private (default)

  public string GetText() { // A function that gets the Text variable
    return Text;            // When this function is called, the Text variable is
  }                         // returned. However, you cannot edit the variable

  public bool shouldSelfDestruct {
    get; // Returns the value of this Property (acts like a variable)
    set; // Allows changes to this Property
  }
  public ClassOne() { shouldSelfDestruct = false; }
}

class ClassTwo {
  private ClassOne classOne = new ClassOne(); // Sets self-destruct variable
                                              // to false. All other variables
                                              // are assigned to the values I set.
  void PrintData() {
    Console.WriteLine("Number equals " + classOne.Number.ToString());
    Console.WriteLine("Text equals " + classOne.GetText());
    Console.WriteLine("Current self-destruct status: " + classOne.shouldSelfDestruct.ToString());
  }
}

Basically, you can’t access a variable that’s marked private like in the example (Text variable). In order to access the private variable in the example, I wrote a function that’s public in order to get it. The same goes for the Property. The ToString at the end of each line is for converting a non-string value to a string. It has nothing to do with data-sharing.

Edit: It seems my efforts have been wasted :frowning:

    Pscript = GetComponent<PlayerScript>();

needs to be

    PlayerScript Pscript = GetComponent<PlayerScript>() as PlayerScript;

At least in the fragment shown, you’ve not created the variable Pscript - it’s a capital crime in C#. You should do:

void Update () 
{
    PlayerScript Pscript = GetComponent<PlayerScript>();
    if(Pscript.isstunned == 0)...