• 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 Puffinzze2 · May 31, 2015 at 08:20 AM · arraystatic

Access Array from static void (C#)

I am having trouble accessing my Transform[] from a static void within the same script.

Example: using UnityEngine; using System.Collections; using UnityEngine.UI;

 public class InventorySlots : MonoBehaviour {
     
     public Transform[] Slots;
 
 
     void Start(){
         Slots = gameObject.GetComponentsInChildren<Transform> ();
     }
 
     void Update () {
     foreach (Transform Slot in Slots) {
             if (!(Slot.name == "InventoryPanel")) {
                 if (Slot.GetComponent<Image> ().sprite == null) {
                     Slot.tag = "EmptySlot";
                     //Slot.gameObject.SetActive (false);
                 } else {
                     Slot.tag = "FilledSlot";
                     //Slot.gameObject.SetActive (true);
                 }
             }
         }
     }
 
     public static void ChangeImage(GameObject ItemObject){
         foreach (Transform Slot in Slots) {
             if(Slot.GetComponent<Image>().sprite == null){
                 Slot.GetComponent<Image>().sprite = Resources.Load ((ItemObject.GetComponent<UID>().UniqueIdentifier).ToString ())as Sprite;
             }
         }
     }
 }

But I keep getting this compiler error:

InventorySlots.cs(29,44): error CS0120: An object reference is required to access non-static member `InventorySlots.Slots'

I was wondering whether or not someone could point me the correct to solve this problem. And also if I change the Transform[] to be a static variable, the script that references the static void shows this error:

NullReferenceException: Object reference not set to an instance of an object InventorySlots.ChangeImage (UnityEngine.GameObject ItemObject) (at Assets/_SCRIPTS/InventorySystem/InventorySlots.cs:30) PickUpFood.Update () (at Assets/_SCRIPTS/_SurvivalMechanics/Food/PickUpFood.cs:20)

If you need anymore information just let me know.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by rageingnonsense · May 31, 2015 at 05:07 PM

This is because you misunderstand how static works. :)

Ok, so you have a static function. What is a static function? It is a function that is independent of a generated object (simple explanation). As such, you call it by using the TYPE, not the variable.

MyType.StaticFunction()

not

MyType myobject = new MyType(); myobject.StaticFunction();

Now what does this mean for you? It means that when you try to reference Slots, which is non static, you are asking the static function to operate on an instance member. How does it know which Slots you want? If you generated 10 of these objects, the static function has no idea which one to use, as they could be different for each object.

So you make it static, and now it is common to all instances of this class. But your second error? Well, kinda need to see how you are calling this method. I tshould be called like so:

InventorySlots.ChangeImage();

but I bet you are doing GetComponent().ChangeImage(), which is incorrect. Why? Because that is an instance of the class, not the class type itself.

Think about other plaes in the engine that use static methods. Vector3.Distance() for instance. You don't do "myVector.Distance()"; you do "Vector3.Distance()".

You should read up a bit on static variables/classes in C#. This is unrelated to Unity really.

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 fafase · May 31, 2015 at 05:37 PM

The idea of method belonging to object is deceitful. When you have 20 objects of a type, you do not have 20 times the method for each object in memory. That would be a waste of space.

Instead the compiler creates one version of the method in the static part of the memory and creates a reference (a variable with the address of the method) for each object.

So doing this:

  public void MyMethod(){}

means you have a reference (4 bytes only) containing the address where the method is stored.

Now how could the compiler be able to access the variables of a specific instance if that method is the same for all?

Well this is because the compiler adds a hidden parameter to your method:

  public void MyMethod(this TypeOfClass objRef){}

so when you do

  myObj.MeMythod();

compiler converts to

  TypeOfClass.MyMethod(myObj);

and that is how you get your access.

Static does pretty much the same except that the compiler does not add the hidden parameter. So the method is left alone in the static memory and hence can only access the static variables and methods.

So in order to access a non-static member within a static class, you need to pass it as a parameter explicitly

 public static MyMethod(TypeHere myInstanceObject){}

this way, the myInstanceObject is refered within the static method, despite not being static as you provide the address while calling the method.

So in your case:

 public static void ChangeImage(GameObject ItemObject, GameObject [] array){
      foreach (Transform Slot in array) {
          var image = Slot.GetComponent<Image>();
          if(image != null && image.sprite == null){ 
                image.sprite = Resources.Load ((ItemObject.GetComponent<UID>().UniqueIdentifier).ToString ())as Sprite;
          }
      }
  }   
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 Puffinzze2 · May 31, 2015 at 06:43 PM 0
Share

Thank you both for the positive feedback, I have learnt a lot from them, and also thanks for resolving my problem. :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GameObjects static array NullReferenceException 1 Answer

Picking Up Collectibles One Time Between Scenes 1 Answer

Static Array 2 Answers

Static array with custom class? 1 Answer

Static array variable 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