x


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 :) 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

more ▼

asked Jul 10 '11 at 09:34 PM

deeredman1991 gravatar image

deeredman1991
16 7 9 10

Or maybe it's because you forgot a typecast at the end.

Jul 10 '11 at 10:23 PM fireDude67
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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)...
more ▼

answered Jul 10 '11 at 10:22 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

Thank you but now I am getting this compile error lol

Assets/Scripts/PlayerAnimation.cs(24,28): error CS1061: Type PlayerScript' does not contain a definition forisstunned' and no extension method isstunned' of typePlayerScript' could be found (are you missing a using directive or an assembly reference?)

Jul 10 '11 at 10:32 PM deeredman1991

oops...nvm i spelled stuned with two n's lol thats why I got the compile error ^_^

Jul 10 '11 at 10:37 PM deeredman1991
(comments are locked)
10|3000 characters needed characters left

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 :(

    Pscript = GetComponent<PlayerScript>();

needs to be

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

answered Jul 10 '11 at 10:19 PM

fireDude67 gravatar image

fireDude67
945 9 15 30

I don't get it...what am I supposed to be looking at?

I marked isstunned as public...but when I try to mark Pscript as public I get...a compile error :|

unexpected symbol compile error to be precise

Jul 10 '11 at 10:27 PM deeredman1991
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4372
x850
x422
x34

asked: Jul 10 '11 at 09:34 PM

Seen: 1469 times

Last Updated: Jul 11 '11 at 04:41 AM