The question is answered, right answer was accepted
How do I pull variables from an object in a list?
Me and a group of classmates and are working on a Spy Hunter clone for a project. I am making the spawner that will drop in our enemies off screen. I am going about this by having each tile of our procedurally generated road contain a script that will contain that tile's borders for the road set. The script for that is as follows (Prepare for unapologetic overuse of comments and whitespace):
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RoadLimits : MonoBehaviour {
 
     [Header("Used to get the unique limits for enemy spawning for each road chunk.")]
     [Tooltip("Uncheck if the road chunk is water")]
     public bool isLand = true;
     [Header("Enemy Spawn width limiters.")]
     [Tooltip("Set to the left-most edge of the top of a road chunk where you want an enemy to spawn.")]
     public float leftSpawnLimit;
     [Tooltip("Set to the right-most edge of the top of a road chunk where you want an enemy to spawn.")]
     public float rightSpawnLimit;
 
 
     //The enemy spawner game object
     private GameObject spawner;
     //Ths script on the above game object
     private AJB_EnemyGenerator enemyGen;
 
 
 
     //Initializes at the beginning
     void Start()
     {
         //Assign the spawner game object and...
         GameObject spawner = GameObject.Find("EnemyGenerator");
         //assign the script component
         enemyGen = spawner.GetComponent<AJB_EnemyGenerator>();
         
         //When I am created, add me to the list in the EnemyGenerator
         enemyGen.roadLimits.Add(this.gameObject.GetComponent<RoadLimits>());
         
     }//End Start()
 
 
 
     //When I am destroyed
     void OnDestroy()
     {
 
         //When I am destroyed, remove the oldest object (me) in the list in the EnemyGenerator
         enemyGen.roadLimits.Remove(gameObject.GetComponent<RoadLimits>());
 
     }//End OnDestroy()
 
 }//End RoadLimits
 
               You can kinda see my logic from this. This is, as you can tell, supposed to work in conjunction with a list in my AJB_EnemyGenerator script which is attached to the game object from which the enemies will spawn. The left and right limits will be the X coordinate while the game object's position will serve as the Y coordinate. My intent is to grab these variables in the other script. To use the correct one, I plan to use the list as a sort of scrolling container that is constantly gaining and losing its contents where I have an int marking a specific position in the list that will line up with the position of the enemy generator. I've been looking around for a way to do this but have yet to find anything. The code from the EnemyGenerator is shown below.
 using System.Collections;
 
               using System.Collections.Generic; using UnityEngine;
public class AJB_EnemyGenerator : MonoBehaviour {
 [Header("The script that controlls the spawning of enemies")]
 public bool isLand = true;
 public bool playerDead = false;
 public bool truckActive = false;
 public List<RoadLimits> roadLimits = new List<RoadLimits>();
 //The number in the list that the enemies will spawn on
 public int _list;
 [SerializeField]
 [Header("These are the enemies that can be spawned using this object")]
 //Array to contain the multiple types of land enemies
 GameObject[] EnemyTypesLand;
 //Array to contain the multiple types of water enemies
 GameObject[] EnemyTypesWater;
 [Header("Timer Variables")]
 //Time variance bounds between generating an enemy
 [SerializeField]
 [Tooltip("Minimum time between enemy spawns")]
 float TimeMin = 1.0f;
 [SerializeField]
 [Tooltip("Maximum time between enemy spawns")]
 float TimeMax = 5.0f;
 //The amount of time to wait between spawning
 //enemies. Populated at spawn time based on a
 //random selection between the min and max.
 private float TimeLimit = 0;
 //Timer for counting elapsed time
 private float Timer = 0;
 [Header("The place where the enemy will spawn")]
 //These will be used to move the spawner left and right so that enemies don't spawn off of the road.
 [SerializeField]
 [Tooltip("Add the limit that cars will spawn on on the left side")]
 //This will be pulled from the road tile's spawn 
 private float leftSpawnLimit;
 [SerializeField]
 [Tooltip("Add the limit that cars will spawn on on the right side")]
 private float rightSpawnLimit;
 // Use this for initialization
 void Start()
 {
     
 }//End Start()
 // Update is called once per frame
 void Update()
 {
     //Initialize the Timer
     UpdateTimer();
     //Initialize SetLimits with a passed in variable
     SetLimits(_list);
     //See how many are in the list so that we can find the right place to put the 
     //spawn point at and then remove this!!
     Debug.Log(roadLimits.Count);
 }//End Update()
 //Change the spawn range for our enemies
 void SpawnLocation()
 {
     if (playerDead || truckActive == true)
     {
         //grab spawn limits of specific array piece
     }
     else
     {
         Debug.Log("I can't find the next spawn position to move to");
     }
 }//End SpawnLocation
 void UpdateTimer()
 {
     //Accumulate time elapsed
     Timer += Time.deltaTime;
     //Reached our time limit
     if (Timer >= TimeLimit)
     {
         //Generate a new time between our bounds
         TimeLimit = Random.Range(TimeMin, TimeMax);
         //Reset the timer
         Timer = 0;
         doSpawn();
     }
 }//End UpdateTimer()
 void doSpawn()
 {
     //Generate a new time between our bounds, but keep the enemy generator's Y and Z
     Vector3 SpawnPos = new Vector3(Random.Range(leftSpawnLimit, rightSpawnLimit) , this.gameObject.transform.position.y, this.gameObject.transform.position.z);
     //Randomly choose an index in the enemy array
     int randIndex = Random.Range(0, EnemyTypesLand.Length);
     if (isLand == true)
     {
         //Use the random index to select one of the enemy
         //types to spawn.
         Instantiate(EnemyTypesLand[randIndex],
                     SpawnPos, transform.rotation);
     }
     else if (isLand == false)
     {
         //Use the random index to select one of the enemy
         //types to spawn.
         Instantiate(EnemyTypesWater[randIndex],
                     SpawnPos, transform.rotation);
     }
     else
     {
         Debug.Log("I con't know if this is a land or water tile");
     }
 }//End doSpawn()
 //Grab the variables from the list object
 void SetLimits( int _int)
 {
     //this.leftSpawnLimit = roadLimits.FindIndex(_int).leftSpawnLimit;
     //this.rightSpawnLimit = roadLimits.FindIndex(_int).rightSpawnLimit;
     //this.isLand = roadLimits.FindIndex(_int).isLand;
 }//End SetLimits()
 
               }//EndEnemyGenerator
The SetLimits function is obviously a bust, but that is sorta what I am trying to do with it.
TL;DR I just want to grab some variables from an indexed item in a list.
Thank you dealing with my novice code, too! I'm sure there are a dozen better solutions to going about this, but I would still like to know how to grab variables from an object in a list either way.
Thank you for your time, A.J.
Also, please disregard the SpawnLocation function. It is obviously missing since I was going to write it after I had this part done.
Thanks again, A.J.
Answer by Haven923 · May 09, 2017 at 09:38 PM
Solution found:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AJB_EnemyGenerator : MonoBehaviour
 {
 
     [Header("The script that controlls the spawning of enemies")]
     public bool isLand = true;
     public bool playerDead = false;
     public bool truckActive = false;
     public List<RoadLimits> roadLimits = new List<RoadLimits>();
     //The number in the list that the enemies will spawn on
     public int _list;
 
 
     [SerializeField]
     [Header("These are the enemies that can be spawned using this object")]
     //Array to contain the multiple types of land enemies
     GameObject[] EnemyTypesLand;
     //Array to contain the multiple types of water enemies
     GameObject[] EnemyTypesWater;
 
     [Header("Timer Variables")]
     //Time variance bounds between generating an enemy
     [SerializeField]
     [Tooltip("Minimum time between enemy spawns")]
     float TimeMin = 1.0f;
     [SerializeField]
     [Tooltip("Maximum time between enemy spawns")]
     float TimeMax = 5.0f;
 
     //The amount of time to wait between spawning
     //enemies. Populated at spawn time based on a
     //random selection between the min and max.
     private float TimeLimit = 0;
     //Timer for counting elapsed time
     private float Timer = 0;
 
     [Header("The place where the enemy will spawn")]
     [SerializeField]
     [Tooltip("The index at which enemies will spawn")]
     private int spawnIndex;
     //These will be used to move the spawner left and right so that enemies don't spawn off of the road.
     [Tooltip("Add the limit that cars will spawn on on the left side")]
     //This will be pulled from the road tile's spawn 
     private float leftSpawnLimit;
     [Tooltip("Add the limit that cars will spawn on on the right side")]
     private float rightSpawnLimit;
 
 
 
     // Use this for initialization
     void Start()
     {
         
     }//End Start()
 
 
 
     // Update is called once per frame
     void Update()
     {
         //Initialize the Timer
         UpdateTimer();
 
         //Initialize SetLimits with a passed in variable
         SetLimits(spawnIndex);
 
         //See how many are in the list so that we can find the right place to put the 
         //spawn point at and then remove this!!
         Debug.Log(roadLimits.Count);
 
     }//End Update()
 
 
 
     //Change the spawn range for our enemies
     void SpawnLocation()
     {
         if (playerDead || truckActive == true)
         {
             //grab spawn limits of specific array piece
         }
         else
         {
             Debug.Log("I can't find the next spawn position to move to");
         }
 
     }//End SpawnLocation
 
 
 
     void UpdateTimer()
     {
 
         //Accumulate time elapsed
         Timer += Time.deltaTime;
 
         //Reached our time limit
         if (Timer >= TimeLimit)
         {
 
             //Generate a new time between our bounds
             TimeLimit = Random.Range(TimeMin, TimeMax);
 
             //Reset the timer
             Timer = 0;
 
             doSpawn();
 
         }
 
     }//End UpdateTimer()
 
 
 
     void doSpawn()
     {
         //Generate a new time between our bounds, but keep the enemy generator's Y and Z
         Vector3 SpawnPos = new Vector3(Random.Range(leftSpawnLimit, rightSpawnLimit) , this.gameObject.transform.position.y, this.gameObject.transform.position.z);
 
         //Randomly choose an index in the enemy array
         int randIndex = Random.Range(0, EnemyTypesLand.Length);
 
         if (isLand == true)
         {
 
             //Use the random index to select one of the enemy
             //types to spawn.
             Instantiate(EnemyTypesLand[randIndex],
                         SpawnPos, transform.rotation);
         }
         else if (isLand == false)
         {
             //Use the random index to select one of the enemy
             //types to spawn.
             Instantiate(EnemyTypesWater[randIndex],
                         SpawnPos, transform.rotation);
         }
         else
         {
             Debug.Log("I con't know if this is a land or water tile");
         }
 
     }//End doSpawn()
 
 
 
     //Grab the variables from the list object
     void SetLimits( int index)
     {
         if (null != roadLimits)
         {
             if (index < roadLimits.Count)
             {
                 this.leftSpawnLimit = roadLimits[index].leftSpawnLimit;
                 this.rightSpawnLimit = roadLimits[index].rightSpawnLimit;
                 this.isLand = roadLimits[index].isLand;
                 
             }
             else
             {
                 Debug.Log("Have list, but index variable is too high.  If occurs at beginning of play, disregard.");
             }
         }
 
         else
         {
             Debug.Log("Can't find RoadLimits");
         }
 
     }//End SetLimits()
 
 }//EndEnemyGenerator
 
              Follow this Question
Related Questions
Removing item from list makes it appear at the bottom ? 1 Answer
item not adding to list correctly! 1 Answer
Dynamic List update 0 Answers
How to do + or - functions to PlayerPrefs.SetInt? 1 Answer
i have a list of transforms and i want to spawn a prefab once for all the transforms in the list. 1 Answer