I am trying to figure out how to simplify creating game objects into a loop. This is as far as I got before becoming stumped on what to do for this to be easier to read.
`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class game_Objects: MonoBehaviour {
public GameObject cube1, cube2, cube3, cube4, cube5, cube6, cube7, cube8, cube9;
public GameObject col1P1, col1P2, col1P3, col1P4, col1P5, col1P6;
public GameObject col2P1, col2P2, col2P3, col2P4, col2P5, col2P6;
public GameObject col3P1, col3P2, col3P3, col3P4, col3P5, col3P6;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyUp (KeyCode.G))
{
GameObject cube1 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube1.transform.position = new Vector3(5f, 0.5f, 0);
GameObject cube2 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube2.transform.position = new Vector3 (6f, 0.5f, 0);
GameObject cube3 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube3.transform.position = new Vector3 (7f, 0.5f, 0);
GameObject cube4 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube4.transform.position = new Vector3 (5f, 1.5f, 0);
GameObject cube5 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube5.transform.position = new Vector3 (6f, 1.5f, 0);
GameObject cube6 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube6.transform.position = new Vector3 (7f, 1.5f, 0);
GameObject cube7 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube7.transform.position = new Vector3 (5f, 2.5f, 0);
GameObject cube8 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube8.transform.position = new Vector3 (6f, 2.5f, 0);
GameObject cube9 =
GameObject.CreatePrimitive (PrimitiveType.Cube);
cube9.transform.position = new Vector3 (7f, 2.5f, 0);
GameObject col1P1 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P1.transform.position = new Vector3 (0, 1f, -5f);
GameObject col1P2 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P2.transform.position = new Vector3 (0, 3f, -5);
GameObject col1P3 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P3.transform.position = new Vector3 (0, 5f, -5f);
GameObject col1P4 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P4.transform.position = new Vector3 (0, 7f, -5f);
GameObject col1P5 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P5.transform.position = new Vector3 (0, 9f, -5f);
GameObject col1P6 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col1P6.transform.position = new Vector3 (0, 11f, -5f);
GameObject col2P1 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P1.transform.position = new Vector3 (0, 1f, 0);
GameObject col2P2 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P2.transform.position = new Vector3 (0, 3f, 0);
GameObject col2P3 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P3.transform.position = new Vector3 (0, 5f, 0);
GameObject col2P4 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P4.transform.position = new Vector3 (0, 7f, 0);
GameObject col2P5 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P5.transform.position = new Vector3 (0, 9f, 0);
GameObject col2P6 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col2P6.transform.position = new Vector3 (0, 11f, 0);
GameObject col3P1 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P1.transform.position = new Vector3 (0, 1f, 5f);
GameObject col3P2 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P2.transform.position = new Vector3 (0, 3f, 5);
GameObject col3P3 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P3.transform.position = new Vector3 (0, 5f, 5f);
GameObject col3P4 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P4.transform.position = new Vector3 (0, 7f, 5f);
GameObject col3P5 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P5.transform.position = new Vector3 (0, 9f, 5f);
GameObject col3P6 =
GameObject.CreatePrimitive (PrimitiveType.Cylinder);
col3P6.transform.position = new Vector3 (0, 11f, 5f);
}
}
}'
Thank you for the assistance and for pointing out better ways I can improve this.
GameObject cube1
etc .. you already have fields with the same name in your class ... These will never be assigned at all since withinUpdate
you "hide" the class fields with new local variables with the same names .... – derHugo