Search Unity

I want to let the ground generate when I click play But it doesn't work.

Discussion in 'Scripting' started by WENKO, May 18, 2019.

  1. WENKO

    WENKO

    Joined:
    Apr 27, 2019
    Posts:
    13

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelGenerator : MonoBehaviour {
    6.  
    7.     // declare the static variables to access Single Turn Class.
    8.     public static LevelGenerator instance;
    9.     // all level pieces blueprints used to copy from
    10.     public List<LevelPiece> levelPrefabs = new List<LevelPiece> ();
    11.     // starting point of the very first level piece
    12.     public Transform levelStartPoint;
    13.     // all level pieces that are currently in the level
    14.     public List<LevelPiece> pieces = new List<LevelPiece> ();
    15.  
    16.     void Awake()
    17.     {
    18.         instance = this;
    19.     }
    20.  
    21.     public void AddPiece()
    22.     {
    23.         // pick the random number
    24.         int randomIndex = Random.Range (0, levelPrefabs.Count-1);
    25.  
    26.         // Instantiate copy of random level prefab and store it in piece variable
    27.         LevelPiece piece = (LevelPiece)Instantiate (levelPrefabs [randomIndex]);
    28.         piece.transform.SetParent (this.transform, false);
    29.  
    30.         // declare the Vector3 Type Variable to stroe the new level position
    31.         Vector3 spawnPosition = Vector3.zero;
    32.  
    33.         // position
    34.         if (pieces.Count == 0) {
    35.             // first piece
    36.             spawnPosition = levelStartPoint.position;
    37.         } else {
    38.             // take exit point from last piece as a spawn point to new piece
    39.             spawnPosition = pieces [pieces.Count - 1].exitPoint.position;
    40.         }
    41.         piece.transform.position = spawnPosition;
    42.         pieces.Add (piece);
    43.     }
    44. }
    Is there problem?

    I attached the file, please.

    Thank you.
     

    Attached Files:

  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,792
    The only thing(in your script) that gets called when you play is Awake, all it does is set "instance" to "this", where is "AddPiece" called?
     
    WENKO likes this.
  3. WENKO

    WENKO

    Joined:
    Apr 27, 2019
    Posts:
    13
    Thank you very much, good day:)