- Home /
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
Or maybe it's because you forgot a typecast
at the end.
Answer by aldonaletto · Jul 10, 2011 at 10:22 PM
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)...
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 for
isstunned' and no extension method isstunned' of type
PlayerScript' could be found (are you missing a using directive or an assembly reference?)
oops...nvm i spelled stuned with two n's lol thats why I got the compile error ^_^
Answer by fireDude67 · Jul 10, 2011 at 10:19 PM
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;
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
Your answer
Follow this Question
Related Questions
Get variable from Script in list and , set direct link to other script 1 Answer
Multiple Cars not working 1 Answer
Getting variables by their names? 3 Answers
Distribute terrain in zones 3 Answers