• 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 Corandy · Jan 05, 2013 at 01:34 PM · getcomponentcomponentstatic

Accessing a list on other script

I have this script:

    #pragma strict
     import System.Collections.Generic;
     DontDestroyOnLoad(gameObject);
     
     var myLifeTime : int = 0;
     public static var survival = new List.<String>();  
     
     function Awake()
     {
         var allGI = GameObject.FindGameObjectsWithTag("GameInfo");
         if(allGI.Length > 1)
         {
             for(theGI in allGI)
             {
                 if(theGI.GetComponent(GameInfo).myLifeTime > myLifeTime)
                 {
                     Destroy(gameObject);
                 }
             }
         }
     }
     
     function Start () {
         myLifeTime++;
     }

But i get this error: Static member `GameInfo.survival' cannot be accessed with an instance reference, qualify it with a type name instead

This is my other script:

 public GUIText scoretitle;
 public GUIText listText;
 GameObject gameInfo;
 private GameInfo other;
     
 void Start()
 {
     gameInfo = GameObject.FindWithTag ("GameInfo");
     other = gameInfo.GetComponent();
     scoretitle.guiText.enabled = true;
     listText.guiText.enabled = true;
     if(other.survival != null)
     {
         for (int i = 0; i <= other.survival.Count -1; i++)
         {
         listText.text += other.survival[i];
         listText.text += "/n";
         }
     }
 }

What is the problem and how can I fix it? :(

Thanks in advance!

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

Answer by Piflik · Jan 05, 2013 at 01:38 PM

Survival is a static variable. That means other.survival doesn't exist, because survival belongs to the class, not to its members. Not sure what your survival list is supposed to do, but you might want to consider making it not static, if you want a separate list for each member of the class.

You can access static variables with ScriptName.VariableName.

Comment
Add comment · Show 3 · 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 Corandy · Jan 05, 2013 at 01:48 PM 0
Share

I have made survival list static because another script is adding objects to the list like:

var other = gameInfo.GetComponent(GameInfo); other.survival.Add(survival);

The reason why it goes first to the gameinfo.js and then to the Score script is because Gameinfo.js is a persistant data script and it needs to have that information so he can send it to the other script if I boot the game up.

I hope I have cleared a bit more up :O But how do I need to use ScriptName.VariableName? Thanks Again

avatar image Piflik · Jan 05, 2013 at 02:02 PM 0
Share

When you only need one single list and have it persistent, a static list is not a bad idea, I just thought to mention it, because many seem to treat the keyword static like an easy method to access variables/methods from other scripts...

Anyways, what you do to access your static list, is simply write 'GameInfo.survival' wherever you need access to that list. Unity will find the script and the variable inside it, even if you haven't assigned this script to any object in the scene.

You don't need a gameObject or the GameInfo component on that object, in fact, if you try to get access to that list that way, you will get the error you are encountering, because you try the non-static approach of accessing other scripts' variables with a static variable and that doesn't work. The component is a member of the class, and so it doesn't have a survival variable. This variable belongs to the class itself.

The method you are trying to use would work with non-static public variables.

avatar image Corandy · Jan 05, 2013 at 02:17 PM 0
Share

Oh many thanks, its working now.

You were right to mention it, because it was not clear why it had a static list and me months ago did overly used static in the wrong way so I understand your urge and importance to mention it.

With this problem now solved I know even more about statics and gives me hope for futher learning. Also the whole compenent thingy is more clearly now.

Thanks alot!

Sorry for bad english

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

9 People are following this question.

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

Related Questions

GetComponent of ALL clones? 2 Answers

enabled is not a member of UnityEngine.Component 1 Answer

Why is this generic "Get component if null" code not working 5 Answers

Using GetComponent in multiple scripts for same component? C# 2 Answers

How to GetComponent from a object inside a parent. 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