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 /
  • Help Room /
This question was closed May 09, 2017 at 09:39 PM by Haven923 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Haven923 · May 09, 2017 at 04:19 AM · c#listsaddremove

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.

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 Haven923 · May 09, 2017 at 05:22 AM 0
Share

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.

1 Reply

  • Sort: 
avatar image
0
Best Answer

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

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

Follow this Question

Answers Answers and Comments

344 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

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


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