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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by zero_equals_zero · Jul 18, 2014 at 12:24 AM · staticpickupnon-static

Adding x amount of energy in another Class issue C#

Hi, after some time spending & searching I have to give up on this one.

On my player I have a c# script named EnergyDrain.cs Which as the name implies drains the player energy over x amount of time.

When the player pickup's an energy boost ( variable ) the player has to gain that amount. So, coming from an actionscript background. I thought to simply add a function that absorbs the boost and thus adding the amount to the overal energy with a max limit. I might be thinking totally wrong here, hench the error, and my question here.

in the pickup collider script once the collider successfully found the player i toss my picked up energy towards the energyDrain script to add that amount.

With the current script i get the warning "An object reference is required to access non-static member" I am quite stuck here as why searches doesn't help me, only confuse me more :)

Here is the relevant code: pickupObjectsTrigger.cs

 public class pickupObjectsTrigger : MonoBehaviour {
     
     
     public GameObject pickupObject; //The object we are picking up
     public string pickupObjectName;
     public GameObject inventoryObject; 
     public float energyPower = 10;
 
 
     void Start () 
     {
         if (pickupObject == null) 
         {
             Debug.LogWarning("No object set for pickup!");
         }    
     }
 
                 
     void OnTriggerStay(Collider other) 
     {        
 
             if(other.CompareTag("Player"))
             {
             
 
             if(pickupObject != null && other.CompareTag("Player") && Input.GetButtonDown("Use"))
             {
 
                 pickupObject.transform.parent = inventoryObject.transform;
                 transform.collider.isTrigger = false;
                 if (pickupObjectName == "energy"){
                     EnergyController.ADD_ENERGY(energyAmount);
                 } 
                 //other pickups
 
             }                                                
     }    
 
 }



EnergyController.cs

 public class EnergyController : MonoBehaviour {
 
     public float energy = 100f;
     public float maxEnergy = 100f;
 
     public float drainspeed;
 
     void Update () {
         if(isOn == true){
 
             if(energy > maxEnergy){
                 energy = maxEnergy;
             }
 
             if (energy <=0)
             {
                 energy = 0;
             } else {
                 energy -= drainspeed*Time.deltaTime;
             }
         }
     }
 
     public static void ADD_ENERGY(int amount){
         energy = Mathf.Clamp(energy+amount, 0, 100);
     }
 
 }
 


Thank you for your insight.

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
0
Best Answer

Answer by robertbu · Jul 18, 2014 at 12:31 AM

There are two primary solutions. First you can make 'energy' static:

   public static float energy = 100f;

The use of statics this way (i.e. to create what is essentially a global function) can lead to problems with maintainable code.

The second solution is to make 'ADD_ENERGEY()' not static. Then you could access it this way:

  GameObject.Find("TheNameOfTheGameObjectThatHasTheEnergyControllerScript").GetComponent<EnergyController>().ADD_ENERGY(enertyAmount);

Other solutions include using SendMessage and Events and Delegates.

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 zero_equals_zero · Jul 18, 2014 at 12:49 AM 0
Share

Thank you so much. The first method i tried and worked, but I wish to see the energy in the inspector as you mentioned and other reasons.

I am honestly a bit baffled. As I tried the second method. Too much whiskey and distraction would have caused my steps into the dark. After going back into my earlier testscript. I saw i left out the ( ) after the GetComponent Then i found some similar questions where people targeted the script directly, hence my approach stated above.

/no more whiskey for me tonight ;)

Once again. Thanks!

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

2 People are following this question.

avatar image avatar image

Related Questions

ERROR - An object reference is required to access non-static member 3 Answers

An instance of type "x" is required to access non-static variable "y" 2 Answers

Assessing variables from other scripts using an accessor seems inelegant. Please explain. 3 Answers

An object reference is required to accsess a non-static member 'Lives.loselife()' 1 Answer

An object reference is required to access non-static member 2 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