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 unityuser13 · Nov 30, 2014 at 08:42 PM · listaccessing from any script

Accessing a list from another script

I have two scripts attached to a gameobject, one of them creates a list of custom objects and then the second script attached to the same gameobject needs to access this list to iterate through it in order to know which objects to display.

I have written the following code example to show the problem I am having.

 using UnityEngine;
 using System.Collections.Generic;
 
 public class TestScript : MonoBehaviour {
 
     public List<int> myList;
 
     void Start () {
 
         myList.Add (7);
         myList.Add (8);
         myList.Add (9);
     }
 
 
     public List<int> GetList(){
         return myList;
         }
 }



 using UnityEngine;
 using System.Collections;
 
 public class TestScript2 : MonoBehaviour {
 
     private GameObject myGameObject;
 
     void Start () {
         myGameObject = this.gameObject;
         TestScript myTestScript1 = myGameObject.GetComponent<TestScript> ();
         foreach (int item in myTestScript1.GetList()){
             print (item);
         }
 
     }
 
 }

I'm not sure why I cannot access the list. I have tried doing it directly as a public variable but still have the same issue.

When I run it in my actual code the objects in the list are gameobjects with textmesh renderers and custom variables. And I also get "Object reference not set to an instance of an Object" errors when I try to access the list of these objects.

I thought it might be because I need to ensure the list creation script is executed before the second script trying to access the list, so I tried adding it to the script execution order settings before with it ordered before the second accessing script. However this didnt' stop the error.

Comment
Add comment · Show 1
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 jenci1990 · Dec 01, 2014 at 01:11 PM 0
Share

It's working for me, and no error. Your scripts was attached at same GameObject?

2 Replies

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

Answer by Landern · Dec 01, 2014 at 01:32 PM

In your example you never instantiate the List of type int.

You should either instantiate the field in line or in the Start method.

// Old script

 using UnityEngine;
 using System.Collections.Generic;
 
 public class TestScript : MonoBehaviour {
   public List<int> myList;
 
   void Start () {
     myList.Add (7);
     myList.Add (8);
     myList.Add (9);
   }
   public List<int> GetList(){
     return myList;
   }
 }



// New script

 using UnityEngine;
 using System.Collections.Generic;
 
 public class TestScript : MonoBehaviour {
   public List<int> myList = new List<int>(); // instantiate here
   
   void Start () {
     // or instantiate the list here
     myList = new List<int>();
     myList.Add (7);
     myList.Add (8);
     myList.Add (9);
   }
 
   public List<int> GetList(){
     return myList;
   }
 }

Be cause you said:

I have tried doing it directly as a public variable but still have the same issue

I also assumed you were not setting the List of int in the inspector.

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 SuperMasterBlasterLaser · Dec 01, 2014 at 02:33 PM

To have access to methods of object, first you must get instance of this object. For example I have ScriptA and ScriptB:

  using UnityEngine;
  using System.Collections.Generic;
  
  public class ScriptA : MonoBehaviour {
  
      public string methodA() {
         return "Method A";
      }
 }

ScriptB:

   using UnityEngine;
   using System.Collections.Generic;
   
   public class ScriptB : MonoBehaviour {
   
       public ScriptA instanceScriptA;
 
       void Start() {
           Debug.Log(instanceScriptA.methodA());
       }
  }

So you must attach some GameObject with ScriptA component to GameObject with ScriptB component.

Second method is to store static singleton instance of your script to have access. ScriptA:

   using UnityEngine;
   using System.Collections.Generic;
   
   public class ScriptA : MonoBehaviour {
       
       public static ScriptA instance;
       
       void Awake() {
          if (instance == null)
             instance = this;
       }
 
       public string methodA() {
          return "Method A";
       }
 
 
  }

ScriptB:

 using UnityEngine;
    using System.Collections.Generic;
    
    public class ScriptB : MonoBehaviour {
    
        
  
        void Start() {
            Debug.Log(ScriptA.instance.methodA());
        }
   }
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

A node in a childnode? 1 Answer

Public Static List Dictionary access from another Script OUT OF RANGE ERROR 1 Answer

Access List from another script 0 Answers

Acces list from another script 2 Answers

A respawn script for enemies. 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