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
1
Question by PlasmaByte · Jun 06, 2012 at 07:32 PM · c#accessglobalarraylistpublic variable

Global array list c#

Hello, I'm sorry I don't quite know how to do code snippets here. I have two scripts, a general sort of one which manages an array list and then a second which I wish to access the array list from.

So in the manager script (called PlayerManager) I have:

 public ArrayList playerList = new ArrayList();
 public class PlayerEntry
 {
     public string name;
     public NetworkPlayer player;
 }

Then in my other script I wish to access this array list and add new entries. The following commands attempt to create a new entry to this array list.

 PlayerManager.PlayerEntry newEntry = new PlayerManager.PlayerEntry();
 newEntry.name = playerName;
 newEntry.player = Network.player;
 PlayerManager.playerList.Add(newEntry);

The first command works where the program is able to create the temporary entry. However, the error appears to be on the final line where I actually attempt to access the list itself.

The error code itself is:

 error CS0120: An object reference is required to access non-static member `PlayerManager.playerList'

The unity script reference implies that this is how you access global variables from another function with script.variable. Does anyone know what I am doing wrong?

Comment
Add comment · Show 3
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 Drakestar · Jun 06, 2012 at 08:36 PM 0
Share

Is the Player$$anonymous$$anager class static?

avatar image PlasmaByte · Jun 06, 2012 at 09:31 PM 0
Share

The class is not static. I make the array list static and it seems to work now. Adding static to the class so that it is:

public static class Player$$anonymous$$anager : $$anonymous$$onoBehaviour {

gives the following error:

error CS0713: Static class Player$$anonymous$$anager' cannot derive from type UnityEngine.$$anonymous$$onoBehaviour'. Static classes must derive from object

I'm not really sure how to make a class static in c#.

Thanks for your help guys. I would up vote your answer fishspeaker but I don't seem to have that ability.

avatar image FishSpeaker · Jun 06, 2012 at 10:16 PM 0
Share

Yeah, if you want Player$$anonymous$$anager to be a $$anonymous$$onoBehaviour, it can't be static. $$anonymous$$onoBehaviours end up being Components on GameObjects, and the whole architecture depends on them being instantiated.

You don't need a static class to make a static variable, though.

You should have enough $$anonymous$$arma to upvote now.

1 Reply

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

Answer by FishSpeaker · Jun 06, 2012 at 09:09 PM

You need to declare playerList as static to access it as you describe:

 public static ArrayList playerList = new ArrayList();

But it may make more sense to keep playerList as an instance variable, and instead access it from a PlayerManager instance. Assuming PlayerManager is a MonoBehaviour on some GameObject, it would look something like:

 gameObject.GetComponent<PlayerManager>().playerList.Add(newEntry);

It all depends on how you have things setup.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problem with Accessing Variables from Other Scripts in c#. 1 Answer

create a dynamic menu 1 Answer

SOLVED Accessing an other C# script and run a function 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