Home > Blockchain >  How do I simplify GameObject key pressed code with Loops?
How do I simplify GameObject key pressed code with Loops?

Time:10-26

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.

CodePudding user response:

The answer lies in the namespace you imported but not using :-) System.Collections.Generic. Use List or something that suits the needs of the application.

Understand the pattern that is occurring in the sequence, e.g. for Cube creation after every 3 values, the X value becomes 5 and Y value increments by 1. For every 3 values we can use a variable like _cubeOffset

So since you have to create 9 cubes, start with XValue = 4.0f and YValue = -0.5f. Now start a loop for creation of 9 cubes by taking i from 0 to 8. First step would be to check if i % 3 == 0, if so, then set XValue = 4.0f and YValue = 0.5f

_cubeOffset = 3;
_startXCube = 4.0f;
_startYCube = -0.5f;

_cubes = new List < GameObject > ( TOTAL_CUBES );
for ( int i = 0; i < TOTAL_CUBES;   i ) {
    /*
     * This below check will run when i = 0, 3, 6
     * at this point we set the _startXCube to 4.0f and
     * increment the previous value of _startYCube by 1.
     * Therefore at i = 0
     * _startXCube = 4.0f
     * _startYCube = 0.5f, since the previous value of _startYCube is -0.5f
     *
     * i = 3
     * _startXCube = 4.0f
     * _startYCube = 1.5f, since the previous value of _startYCube is 0.5f
     * 
     * i = 6
     * _startXCube = 4.0f
     * _startYCube = 2.5f, since the previous value of _startYCube is 1.5f
     */
    if ( i % _cubeOffset == 0 ) {
        _startXCube = 4.0f;
        _startYCube  = 1.0f;
    }
    _startXCube  = 1.0f;    // Incrementing _startXCube since we need 5.0f, 6.0f and 7.0f values
    GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cube );
    gb.transform.position = new Vector3 ( _startXCube, _startYCube, 0.0f );
    _cubes.Add ( gb );
    Debug.Log ( $"<color=lightblue>Creating Cube -> X:{_startXCube} Y:{_startYCube} Z:0</color>" );
}

Similarly, we can create the logic for the Cylinders as below:

_startZCylinder = -10.0f;
_cylinders = new List < List < GameObject > > ( TOTAL_COLUMNS );
for ( int i = 0; i < TOTAL_COLUMNS;   i ) {
    _cylinders.Add ( new List < GameObject > ( TOTAL_CYLINDERS ) );
    _startYCylinder = -1.0f;
    _startZCylinder  = 5.0f;
    for ( int j = 0; j < TOTAL_CYLINDERS;   j ) {
        _startYCylinder  = 2.0f;
        GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cylinder );
        gb.transform.position = new Vector3 ( 0.0f, _startYCylinder, _startZCylinder );
        _cylinders [ _cylinders.Count - 1 ].Add ( gb );
        Debug.Log ( $"<color=lightblue>Creating Cylinder -> X:{0.0f} Y:{_startYCylinder} Z:{_startZCylinder}</color>" );
    }
}

Kindly do note, for these code snippets I have used List data structure as below:

private List < GameObject > _cubes;
private List < List < GameObject > > _cylinders;

This is the whole script

using System.Collections.Generic;

using UnityEngine;

public class CreatePrimitive : MonoBehaviour {
    
    #region Private Field
    private int _cubeOffset;
    private float _startXCube;
    private float _startYCube;
    private List < GameObject > _cubes;

    private int _cylinderOffset;
    private float _startYCylinder;
    private float _startZCylinder;
    private List < List < GameObject > > _cylinders;

    private const int TOTAL_CUBES = 9;
    private const int TOTAL_COLUMNS = 3;
    private const int TOTAL_CYLINDERS = 6;
    #endregion Private Field
    
    #region MonoBehaviour Callback
    private void Start () {
        _cubeOffset = 3;
        _startXCube = 4.0f;
        _startYCube = -0.5f;

        _cubes = new List < GameObject > ( TOTAL_CUBES );
        for ( int i = 0; i < TOTAL_CUBES;   i ) {
            /*
             * This below check will run when i = 0, 3, 6
             * at this point we set the _startXCube to 4.0f and
             * increment the previous value of _startYCube by 1.
             * Therefore at i = 0
             * _startXCube = 4.0f
             * _startYCube = 0.5f, since the previous value of _startYCube is -0.5f
             *
             * i = 3
             * _startXCube = 4.0f
             * _startYCube = 1.5f, since the previous value of _startYCube is 0.5f
             * 
             * i = 6
             * _startXCube = 4.0f
             * _startYCube = 2.5f, since the previous value of _startYCube is 1.5f
             */
            if ( i % _cubeOffset == 0 ) {
                _startXCube = 4.0f;
                _startYCube  = 1.0f;
            }
            _startXCube  = 1.0f;    // Incrementing _startXCube since we need 5.0f, 6.0f and 7.0f values
            GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cube );
            gb.transform.position = new Vector3 ( _startXCube, _startYCube, 0.0f );
            _cubes.Add ( gb );
            Debug.Log ( $"<color=lightblue>Creating Cube -> X:{_startXCube} Y:{_startYCube} Z:0</color>" );
        }

        _startZCylinder = -10.0f;
        _cylinders = new List < List < GameObject > > ( TOTAL_COLUMNS );
        for ( int i = 0; i < TOTAL_COLUMNS;   i ) {
            _cylinders.Add ( new List < GameObject > ( TOTAL_CYLINDERS ) );
            _startYCylinder = -1.0f;
            _startZCylinder  = 5.0f;
            for ( int j = 0; j < TOTAL_CYLINDERS;   j ) {
                _startYCylinder  = 2.0f;
                GameObject gb = GameObject.CreatePrimitive ( PrimitiveType.Cylinder );
                gb.transform.position = new Vector3 ( 0.0f, _startYCylinder, _startZCylinder );
                _cylinders [ _cylinders.Count - 1 ].Add ( gb );
                Debug.Log ( $"<color=lightblue>Creating Cylinder -> X:{0.0f} Y:{_startYCylinder} Z:{_startZCylinder}</color>" );
            }
        }
    }
    #endregion MonoBehaviour Callback
}

CodePudding user response:

You can create a static function, just like this

public class CubeHelper
{
     public static GameObject Creating(Vector3 position, PrimitiveType primitiveType)
     {
          GameObject createdGameObject = GameObject.CreatePrimitive(primitiveType)
          createdGameObject.transform.position = position;
          return createdGameObject;
      }
}

and then just passing parameter :

Gameobject sthg = CubeHelper.Creating("your vector3", "your primitive type");
  • Related