Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Topthink · Sep 02, 2017 at 12:39 AM · c#script.listvariablesaccess

How Do I Access and Change Items in a List on Another Script?

I create a list of items in this script...works fine to create units which are small flat cylinders that look like buttons.

 public class SpawnAll : MonoBehaviour {
 
     public static int friendCounter01 = 0;
     public GameObject CombatUnit;
     public List<FriendlyCombatUnit> friendlyCombat = new List<FriendlyCombatUnit> ();
 
 
     void Awake () {
         for (int i = 0; i < 5; i++) {
             SpawnFriendlyCombatUnit ();
         }
     }
 
     void SpawnFriendlyCombatUnit(){
     
         Instantiate (CombatUnit);
         CombatUnit.transform.position = new Vector3 (Random.Range (5f, 25f), .5f, Random.Range (5f, 25f));
         string tempName = (friendCounter01 * 111).ToString (); // Placeholder
         int tempOne = (int) Random.Range(95f,106f);
         int tempTwo = (int)Random.Range (45f, 56f);
         int tempThree = (int)Random.Range (20f, 31f);
         int tempNum = friendCounter01;
         friendlyCombat.Add(new FriendlyCombatUnit(tempNum, tempName, tempOne, tempTwo, tempThree));
 
         friendCounter01++;
         Debug.Log (friendCounter01);
     }
 }
 
 public class FriendlyCombatUnit{
 
     public int unitID { get; set; }
     public string unitName { get; set; }
     public int unitStrength { get; set; }
     public float unitOrganization { get; set; }
     public int unitExperience { get; set; }
 
     public FriendlyCombatUnit(int i, string n, int s, float o, int e)
     {
         unitID = i;
         unitName = n;
         unitStrength = s;
         unitOrganization = o;
         unitExperience = e;
     }
  }


I'd like to access the List information from another script. I've tried about everything I know of and researched about everything I can think of online but cannot find anything on point.

 public class Movement : MonoBehaviour {
 
     private bool isSelected = false;
     public NavMeshAgent agent;
     SpawnAll s1;
 
     void Awake(){
     
         s1 = GetComponent<SpawnAll> ();
 
     
     }
 
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast (ray, out hit)) {
                 Collider col = hit.collider;
 
                 if (col.gameObject.tag == "Friendly")  /// /// /// Begin Tag/Friendly section ////////////////////////////////////////////// 
                 {
 
                     GameObject[] tempGo;  ///  This "tempGo" section works to turn all units Blue and to change "isSelected" to false //////////////////////////////////////////
                     tempGo = GameObject.FindGameObjectsWithTag ("Friendly");
                     foreach (GameObject thing in tempGo) {
                         thing.GetComponent<Renderer> ().material.color = Color.blue;
                         isSelected = false;
                     } /// /// /// ///////////////////////////////////////////////////////////  End "tempGo" Section  ///////////////////////////////////////////////////////////
 
                     isSelected = true;
                     agent = col.gameObject.GetComponent<NavMeshAgent> ();
                     col.gameObject.GetComponent<Renderer>().material.color = Color.cyan;

                 } /// /// /// End tag/Friendly section ///////////////////////////////////////////////////////////////////////////////////////
 
                 if (col.gameObject.name == "Terrain" && isSelected == true){
 
                     //Debug.Log (hit.point);
                 }
 
                 if (col.gameObject.name == "Terrain") {
                     //Debug.Log (hit.point);
                 }
             }
         }
 
         if (Input.GetMouseButtonDown (1) && isSelected == true) {
             Ray ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit1;
             if (Physics.Raycast (ray1, out hit1)) {
                 Collider col = hit1.collider;
                 agent.SetDestination (hit1.point);
                 isSelected = false;
                 gameObject.GetComponent<Renderer>().material.color = Color.blue;
             }
         }
     }/// /// ///  Last bracket of "Update" method. 
 }

Anyway, I'm stumped as to how to access the List in the script SpawnAll from another script.

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 Topthink · Sep 02, 2017 at 12:45 AM 0
Share

Around Line 35 is where I want to put the code to access the info ... I want to be able to access the info by mouse clicking on each unit/object which will then display info on a GUI.

I'm still working on how to get the index number of the list from just a mouse click so that I can access the remainder of the information about that unit/object by utilizing the index of that item...but that is really a separate question unless someone feels compelled to answer that here (which I would greatly be thankful for).

Thanks in advance for any help.

2 Replies

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

Answer by TheSOULDev · Sep 02, 2017 at 12:51 AM

Simply make the list static (then you can access it by ClassName.ListName), or initialize an instance of the script containing your list into the script you want to access it from. It is no different than editing a value.

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 Topthink · Sep 02, 2017 at 04:13 PM 0
Share

Thank you, I tried making it a static but I must have done something incorrectly...I will attempt this again per your instructions to see if it fits the bill.

I appreciate your help.

avatar image Topthink · Sep 02, 2017 at 04:42 PM 0
Share

I worked on this all day yesterday and I swear I thought I tried everything including making the list static (I must have been trying to access it wrong or had slightly incorrect syntax or something)...but your suggestion seems to work perfectly today.

...and I'm not kidding when I said I worked on it all day. Sometimes a new day and a good suggestion can make a world of difference.

I'm continuing to evaluate some of the other items below but right now I'm pretty happy.

avatar image TheSOULDev Topthink · Sep 02, 2017 at 09:24 PM 0
Share

You should keep in $$anonymous$$d that while static is really a one-hit wonder solution, you should use static variables sparingly. You should use static variables for something that only one instance can exist of, something that is constantly being accessed by plenty of other scripts and something that is possibly not a security issue. If you need to access your list in only one script without any indication you'll ever call it from another script, then initializing and instance of the mother script of the list is a much better idea.

avatar image
0

Answer by MaxGuernseyIII · Sep 02, 2017 at 01:01 AM

@TheSOULDEv is correct. You can also factor the list out into its own class, neither part of SpawnAll nor Movement. Then you can make that class a singleton (which you can look up in about a million places). Both those solutions amount to having a global variable in your code.

If you do not want global variables, you can create a public field of type SpawnAll on your Movement behavior and access members of that field. Then, in the inspector, you can drag the GameObject with the SpawnAll script into the editor field that creates.

Comment
Add comment · Show 2 · 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 Topthink · Sep 02, 2017 at 04:14 PM 0
Share

Thank you too...I will look at both your suggestions to see if they match my needs.

I really appreciate that you took the time to help me.

avatar image Topthink · Sep 02, 2017 at 04:48 PM 0
Share

I'm still looking at both your suggestions. I'm especially interested in your "singleton" comment and I'm going to try to track down a couple of those "million places", if for nothing else, just to learn something that might help me in the future.

I'm not totally sure if I fully understand your second paragraph but I will also try to explore that before I move forward.

Thank you again for your help.

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

381 People are following this question.

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

Related Questions

How do I make a list of references to a variable? 2 Answers

How to access List<> from other script? 1 Answer

Last time I'll ask for help on this lol 2 Answers

how can i check if there is a blanc spot in my list 3 Answers

Multiple Cars not working 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