Im learning unity by making changes to a game framework I bought off the asset store.
The game has range warnings that activate based on the enemy spawner. These are just lines that show the direction the enemy is attacking from. It was initially set manually in unity and aligning them was a pain. So I wrote a script to draw lines using LineRenderer for all spawnpoints. This works well but I am struggling with replacing the old manually set range warnings with my lines that I wrote in code.
[System.Serializable]
//spawnpoint variables
public class spawnpoint
{
public Transform spawnPoint;
public GameObject warning;
}
public class Spawner : MonoBehaviour
{
//variables not visible in the inspector
[HideInInspector]
public List<spawnpoint> spawnpoints;
[HideInInspector]
public List<enemy> enemies;
//variables visible in the inspector
//public Vector3 towerPosition; //not used anymore in this version of spawner
public float startWait;
public GameObject newEnemyButton;
public Animator newEnemyPopupAnimator;
public Transform[] spawnPoints;
public Transform towerPos;
public int startPoint;
//not visible in the inspector
[HideInInspector]
public bool showRangeWarning;
[HideInInspector]
public GameObject defaultEnemy;
GameObject[] myLines;
GameObject targetEnemy;
Quaternion targetRotation;
IEnumerator Start()
{
//Debug.Log("Count: " + spawnpoints.Count);
//update the range warnings immediately
if (showRangeWarning)
{
DrawLines();
updateRangeWarning(0);
}
}
void DrawLines()
{
myLines = new GameObject[spawnpoints.Count];
Vector3 startPos = towerPos.transform.position;
// Cache your material so you don't need to make so many copies.
var material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
for (int i = 0; i < spawnpoints.Count; i++)
{
// FIRST: Create & assign your GameObject into the array slot.
var lineObject = new GameObject("Line" + i); // also gameobjects are created and named so that in Unity we see Line0, Line1, Line2 and Line3
Debug.Log(lineObject);
myLines[i] = lineObject;
lineObject.transform.position = startPos;
LineRenderer lr = lineObject.AddComponent<LineRenderer>();
// No need to search for the component we just added!
// LineRenderer lr = myLines[i].GetComponent<LineRenderer>();
// Use sharedMaterial to be batching-friendly and avoid excess allocation.
lr.sharedMaterial = material;
lr.SetColors(Color.red, Color.blue);
lr.SetWidth(0.3f, 0.3f);
lr.SetPosition(0, startPos);
lr.SetPosition(1, spawnpoints[i].spawnPoint.transform.position);
}
//List<GameObject> drawnLines = myLines;
}
all lines are made using DrawLines but the initial way it was set up using warning within this class:
public class spawnpoint
{
public Transform spawnPoint;
public GameObject warning;
}
Since the warning variable is not an array or a list how do I assign my array to that gameobject, also it is somehow nested to all the spawnpoints, as can be seen from the debugger window:
Here is how the warnings are dragged under each spawnpoint in unity:

