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 Cherikov · Aug 23, 2013 at 07:31 AM · c#float

C# Changing Float with External Script

hey guys i need help with this error. I'm using this accessor code as a test to see if i can access a float value in my hunger code as if i was eating food:

//this code is on the food btw

 using UnityEngine;
 using System.Collections;
 
 public class Accessor : MonoBehaviour {
     void Start()
     {
         GameObject Player = GameObject.Find("Player");
         Hunger hunger = Player.GetComponent<Hunger>();
         Hunger.hungerLevel += 1.0f;
     }
 }

this is my hunger script:

 using UnityEngine;
 using System.Collections;
 
 public class Hunger : MonoBehaviour {
     
     
     public float hungerLevel;
     
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         hungerLevel -= Time.deltaTime;
     
     }
 }

when i try it it gives me this error:

Assets/Accessor.cs(9,16): error CS0120: An object reference is required to access non-static member Hunger.hungerLevel' and when i make the float static it gives me this error: ssets/PlayerHealth.cs(33,55): error CS0176: Static member Hunger.hungerLevel' cannot be accessed with an instance reference, qualify it with a type name instead.

the piece of code its reffering to in my player Health script is this:

 void Update () {
          AddjustCurrentHealth(0);
 
         
         if (thirst.thirstLevel <= 0 || hunger.hungerLevel <= 0)
     {
     curHealth = curHealth - 0.01f; // change the 10 to whatever damage you want
     }
     }
 

What do I do?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Sajidfarooq · Aug 23, 2013 at 07:52 AM

Change this line in the Accessor script:

  Hunger.hungerLevel += 1.0f;  

To this:

 hunger.hungerLevel += 1.0f; 


Basically, you are trying to access the class (Hunger), whereas you need to access the object you just created earlier (hunger).

Comment
Add comment · Show 6 · 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 Cherikov · Aug 23, 2013 at 07:53 AM 0
Share

ok ill try that

avatar image Cherikov · Aug 23, 2013 at 07:55 AM 0
Share

umm it didnt increase it...

avatar image Cherikov · Aug 23, 2013 at 07:56 AM 0
Share

or decrease it

avatar image Sajidfarooq · Aug 23, 2013 at 09:00 AM 0
Share

That is a different problem for which you should post a separate question (with the rest of the code, i.e, AddjustCurrentHealth() etc)

$$anonymous$$y answer was for your question about error CS0120, which was caused by what I explained above.

avatar image VioKyma · Aug 23, 2013 at 10:50 AM 0
Share

To avoid this error in future, I would suggest using variable names that start with a lower case letter.

Show more comments

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Distribute terrain in zones 3 Answers

Float not working all the time? 1 Answer

for loop error 1 Answer

Multiple Cars not working 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