Search Unity

Game Object does not appear when play [Beginner]

Discussion in '2D' started by laeti_tok, Apr 2, 2021.

  1. laeti_tok

    laeti_tok

    Joined:
    Apr 2, 2021
    Posts:
    2
    Hi everyone!

    I'm a beginner in C#/Unity. For my first training, I decided to follow this tutorial:
    . I have faced a few issues and this one is quite time consuming .. So I would need your help( please!!)
    I have created pipe spawner. However, the pipes do not appear on the screeen when I hit the button play. Pipes clone are created by object pooling in the hierarchy but the inspector show that they are not ticked so they don't appear on the screen. This can be seen on the picture below.


    I would like to have the pipes shown and therefore automatically ticked when the game starts. I don't understand how I could solve this issue. :oops:
    The tutorial creator does not face it.
    I added two code files to this message.

    Thanks a lot for your help !!!
     

    Attached Files:

  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    use gameobject.setactive(true) to tick them on
     
  3. laeti_tok

    laeti_tok

    Joined:
    Apr 2, 2021
    Posts:
    2
    thanks a lot for for your quick answer :) I'm a little bit puzzled because there are no such line in the code of the creator though he doesn't have the problem. Could you please tell me where i could add such line in my pool object spawner class called Parallexer:

    Code (CSharp):
    1.     class PoolObject{
    2.         public Transform transform;
    3.         public bool inUse;
    4.         public PoolObject(Transform t){ transform = t;}
    5.         public void Use(){ inUse =true;}
    6.         public void Dispose(){ inUse=false;}
    7.     }
    8.  
    9.     [System.Serializable]
    10.     public struct YSpawnRange{
    11.         public float min;
    12.         public float max;
    13.         }
    14.  
    15.     public GameObject Prefab;
    16.     public int poolSize;
    17.     public float shiftSpeed;
    18.     public float spawnRate;
    19.  
    20.     public YSpawnRange ySpawnRange;
    21.     public Vector3 defaultSpawnPos;
    22.     public bool spawnImmediate;//particle prewarm
    23.     public Vector3 immediateSpawnPos;
    24.     public Vector2 targetAspectRatio;
    25.  
    26.     float spawnTimer;
    27.     float targetAspect;
    28.     PoolObject[] poolObjects;
    29.     GameManager game;
    30.  
    31.     void Awake(){
    32.         Configure();
    33.     }
    34.  
    35.     void Start(){
    36.         Debug.Log("Parallaxer Start");
    37.        game=GameManager.Instance;
    38.     }
    39.  
    40. void OnEnable(){
    41.     GameManager.OnGameOverConfirmed+=OnGameOverConfirmed;
    42. }
    43.  
    44. void OnDisable(){
    45.  
    46.     GameManager.OnGameOverConfirmed-=OnGameOverConfirmed;
    47.     }
    48. void OnGameOverConfirmed(){
    49.     for(int i=0; i< poolObjects.Length;i++){
    50.         poolObjects[i].Dispose();
    51.         poolObjects[i].transform.position=Vector3.one*1000;
    52.     }
    53.       if(spawnImmediate){
    54.         SpawnImmediate();
    55.     }
    56.     // Configure();
    57.  
    58.     }
    59.  
    60. void Update(){
    61.    // if(game.GameOver) return;
    62.  
    63.     Shift();
    64.     spawnTimer+=Time.deltaTime;
    65.     if(spawnTimer>spawnRate){
    66.         Spawn();
    67.         spawnTimer=0;
    68.     }
    69.  
    70. }
    71.  
    72. void Configure(){
    73.     Debug.Log("Parallaxer Configure");
    74.     targetAspect=targetAspectRatio.x/targetAspectRatio.y;
    75.     poolObjects=new PoolObject[poolSize];
    76.     for (int i=0;i<poolObjects.Length;i++){
    77.         GameObject go= Instantiate(Prefab) as GameObject;
    78.         Transform t = go.transform;
    79.         t.SetParent(transform);
    80.         t.position=Vector3.one *1000;
    81.         poolObjects[i]= new PoolObject(t);
    82.     }
    83.  
    84.     if(spawnImmediate){
    85.         SpawnImmediate();
    86.     }
    87.  
    88. }
    89. void Spawn(){
    90.     Debug.Log("Parallaxer Spawn");
    91.     Transform t= GetPoolObject();
    92.     if(t==null) return;//if trie, this indicates that poolSize is too small
    93.     Vector3 pos=Vector3.zero;
    94.     pos.x=defaultSpawnPos.x;
    95.     pos.y=Random.Range(ySpawnRange.min,ySpawnRange.max);
    96.     t.position=pos;
    97. }
    98.  
    99. void SpawnImmediate(){
    100.       Transform t= GetPoolObject();
    101.     if(t==null) return;//if trie, this indicates that poolSize is too small
    102.     Vector3 pos=Vector3.zero;
    103.     pos.x=immediateSpawnPos.x;
    104.     pos.y=Random.Range(ySpawnRange.min,ySpawnRange.max);
    105.     t.position=pos;
    106.     Spawn();
    107.  
    108.  
    109. }
    110.  
    111. void Shift(){
    112.     for (int i=0;i<poolObjects.Length;i++){
    113.         poolObjects[i].transform.localPosition+=-Vector3.right*shiftSpeed*Time.deltaTime;
    114.         CheckDisposeObject(poolObjects[i]);
    115.     }
    116.  
    117. }
    118. void CheckDisposeObject(PoolObject poolObject){
    119.     if (poolObject.transform.position.x< -defaultSpawnPos.x){
    120.         poolObject.Dispose();
    121.         poolObject.transform.position=Vector3.one*1000;
    122.     }
    123.  
    124. }
    125.  
    126. Transform GetPoolObject(){
    127.     for(int i=0;i<poolObjects.Length;i++){
    128.         if (!poolObjects[i].inUse){
    129.             poolObjects[i].Use();
    130.             return poolObjects[i].transform;
    131.         }
    132.     }
    133.     return null;
    134. }

    thank you :)