Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by deeredman1991 · Jul 10, 2011 at 09:34 PM · c#variablegetcomponentcs0103

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fireDude67 · Jul 10, 2011 at 10:23 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)...
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deeredman1991 · Jul 10, 2011 at 10:32 PM 0
Share

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

avatar image deeredman1991 · Jul 10, 2011 at 10:37 PM 0
Share

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

avatar image
1

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deeredman1991 · Jul 10, 2011 at 10:27 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

script with individual function for each gameObject or public script variable to solve inventory system problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges