Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 RuneyWolf · Sep 04, 2013 at 11:50 PM · c#variable

Accessing a variable from another script C#

Thanks in advance for reading my question and offering help.

I'm getting a bit frustrating as this has stalled my progress for far to long. I'm trying to access one variable from one script and modify it in another. Any time I try a solution I find here, it gives me an error of some sort. The least broken version I have will be posted here along with the error.

Here is the script with the float 'playerThirst' which is what I need to modify:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerNeeds : MonoBehaviour {
     
     public static float playerThirst = 100;
     public int playerExhaustion = 100;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     playerThirst -= Time.deltaTime / 30;
     }
     
     void OnGUI(){
         GUI.Box (new Rect (0,0,100,30), "Thirst: " + playerThirst.ToString("F1") + "/100");
     }
 }

Here is the script that is broken. It has one purpose and that is to modify 'playerThirst':

 using UnityEngine;
 using System.Collections;
 
 public class CactusInteract : MonoBehaviour {
     
     public bool hasBeenDiscovered = false;
     public float cactusWaterSupply = 100;
     public bool collideWithCactus = false;
     float thirsty = PlayerNeeds.playerThirst;
     int thirstSubtract = 1;
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown(KeyCode.E)){
             cactusWaterSupply --;
             thirsty += thirstSubtract;
         }
     
     }

error: Assets/CactusInteract.cs(9,37): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `PlayerNeeds.playerThirst'

If someone could please, give me a detailed explanation on how to fix this I would be greatly thankful.

Comment
Add comment · Show 2
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 syclamoth · Sep 04, 2013 at 11:52 PM 0
Share

$$anonymous$$ove the initialisation into 'Start'.

avatar image RuneyWolf · Sep 05, 2013 at 12:18 AM 0
Share

Thank you so much! You sir are a genius and I love you.

2 Replies

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

Answer by Vonni · Sep 05, 2013 at 12:20 AM

 public PlayerNeeds playerNeedsReference; // Link in inspector
 float thirsty = 0;
 
 void Start () {
 
 thirsty = playerNeedsReference.playerThirst;
 
 }
 
 // Or set playerThirst in playerNeedsReference to static and simply call it when you need it without having a duplicate varible for thirst, like so:
 
 // .../
        if(Input.GetKeyDown(KeyCode.E)){
          cactusWaterSupply --;
          PlayerNeeds.playerThirst += thirstSubtract;
        }
 // /...
Comment
Add comment · 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
0

Answer by Aarlangdi · Mar 26, 2014 at 04:49 AM

Guys,check this out, It has explained in very simple example about how to access variables from other classes --- http://aarlangdi.blogspot.com.au

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 whydoidoit · Mar 26, 2014 at 04:50 AM 0
Share

But only one very specific way of doing it that requires global variables. http://unitygems.com/gotchas

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editing a variable from another script on collision 3 Answers

Using a script as a member variable in another script. 2 Answers

How to access gameObject variable script 2 Answers

C# How to access a variable in a script from an instance with the same script? 1 Answer

Change a Variable with another script not working (C#) 4 Answers


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